ListView Not Displaying Added Text - vb.net

Background:
Hello everyone, I'm not new to visual basic or programming. I just find it the fastest to make a GUI in visual basic for various programs; however, I'm having a recent issue.
I have a form with a ListView control that I populate using a textbox and a button.
the code I use for the button is:
Code:
listview1.items.add(textbox2.text)
Pretty simple code that has always worked. I changed a few properties on my ListView as well, and I will list them. I just don't know what is going on.Here's a list of the properties changed (everything else is default):
Properties:
BackColor = DarkGray
BorderStyle = FixedSingle
HeaderStyle = None
MultiSelect = False
View = Details
All properties were changed via the properties tab and not through code. I want to be able to add text to the control. I'm not adding any subitems either. I'm just confused because this code has always worked, and I've never had this issue. I'm also using Visual Basic 2010 Express Edition if that helps.

If the added item in ListView is not displaying then check the ListView.View property. if this property has set to Detail view then Columns must be added in that listView control otherwise added item will not display.
Make sure item is added correctly.
Dim item As ListViewItem = listView1.Items.Add("Item Text") ' This text will be displayed in first column. The column index will be 0
' Further values will be added in SubItem
item.SubItems.Add("Sub Item1")
item.SubItems.Add("Sub Item2")
item.SubItems.Add("Sub Item3")

Related

Getting Selected Items from a Listbox

Good Wednesday All.
I am running into a brick wall (easy for a shade tree coder to do) I have a Listbox that i populated with a datatable. I want to get the all LicenseID's from the selected items. In other words, if the user selects 3 out of 8 of the list box, I need to get the LicenseID for each of those 3.
Below is how I populated the listbox
Using cmd As New OleDbCommand(cmdText, conn)
conn.Open()
Dim reader As OleDbDataReader = cmd.ExecuteReader()
dt.Load(reader)
ListBox1License.DataSource = dt
ListBox1License.DisplayMember = "InstitutionTypeAbrev"
ListBox1License.ValueMember = "LicenseID"
End Using
I need to get the selected items from the listbox to use later.
I am thinking of adding the selected Items to an array.
I have searched around STackOverflow for some examples but none seem to work for me.
Any Help Appreciated
I'll show you how to derive the answer for this yourself:
I've set up a form:
Really simple; the listbox is like your listbox. The button is just there to give me an easy way to stop the code and examine what is going on.
I wrote some code to populate some things into my listbox. It's a screenshot because it doesn't matter that you have exactly this code, so you don't need to write this code (hence why I'm making it hard to copy paste):
I've double clicked my button to make a click handler. I haven't written any code, but I have put a breakpoint on the method declaration - see it's red? Click the margin where the dot is, to put breakpoints in your code. When you hit them, the code stops and waits for you to inspect:
I've run my app and clicked my button. The code has stopped and VS has switched to showing me the code, not the app:
I can now point to some variable that is in scope (like ListBox1) and see a tooltip, or I can open the Locals/Autos windows and see variables that are in scope and drill into them:
Expand you ListBox in the Autos/Locals window. It has a lot of properties. Scroll to SelectedItems:
SelectedItems is a collection of things.. We can tell partly because Microsoft is good at naming collections of things with a plural name, and because the inspector says "enumerate the enumerable" .. it means that it is a bunch of things that we can ForEach to look through
Expanding it we see that my selecteditems has only one thing selected (i truly did only have one selected item in my list when I clicked the button)
We can see that an entry in the SelectedItems collection is a DataRowView type of object. We can see that a DataRowView has a Row property that is a DataRow.. This Row is the DataRow in the DataTable to which the list is bound (you set the DataSource to a DataTable; this is a row from that table).
Every time you dig into the tree another level, that's like using either a dot or an indexer in your code. At this level we've gone listbox1.SelectedItems(0).Row..
So from this we can see that we need a code like:
' we will "enumerate the enumerable"
For Each drv as DataRowView in listbox1.SelectedItems
Dim originalRow = drv.Row 'we could do this to get the row...
Dim selectedAnimaId = row("AnimalID") ' ..and then index the row to get the animal ID ..
Dim selectedAnimalId = drv("AnimalID") ' ... or it's actually possible to index a DataRowView directly, so you can skip the row part
Next drv
It can be handy to write code while you're stopped on a breakpoint so you can look at the values of things as you're writing, and check you're going in the right direction. You might need to use F10 (or whatever key is associated with "step over"/"step into") to move the yellow bar along and execute code lines one by one:
You can only move the code execution along if you've written complete, legal code, but it doesn't have to be logically correct. You can back up and execute again by dragging the yellow arrow in the margin (or right clicking and choosing Set Next Statement). Here I've put some dummy statement in to move along to, so i can check that my animalID is correctly set in X like I expect . I point to X to see the value:
The standard ListBox won't help you with that, past getting the DataRowView objects from the SelectedItems collection. As an alternative, here's a custom control that you can use in place of a standard ListBox that will help you:
Public Class ListBoxEx
Inherits ListBox
Public Function GetItemValue(item As Object) As Object
Dim index = Me.Items.IndexOf(item)
If (index <> -1 AndAlso Me.DataManager IsNot Nothing) Then
Return Me.FilterItemOnProperty(Me.DataManager.List(index), Me.ValueMember)
End If
Return Nothing
End Function
End Class
You can then call GetItemValue and pass any item and get the same value back as you would if that was the SelectedItem and you got the SelectedValue. To get all the values in an array:
Dim licenseIDs = myListBoxEx.SelectedItems.
Cast(Of Object)().
Select(Function(o) CInt(myListBoxEx.GetItemValue(o)).
ToArray()
For more information, see here.
In case you're unaware, if you add a class to your project and it is a control or component, once you build, it will appear automatically at the top of the Toolbox window.
If you already have a standard ListBox in place and you don't want to have to delete it and add a new control, you can edit the designer code file by hand to change the existing control. To do that, open the Solution Explorer and select a node within your project, click the Show All Files button, expand the node for your form, double-click the designer code file and then replace ListBox with ListBoxEx (or whatever you call it) in the relevant places. I'd advise creating a backup copy or syncing with source control first, in case you mess it up.

VB - Reference Control via a string stored in My.Settings

I'm making an extremely simple app which has multiple screens (as Panels) that are hidden/shown clicking buttons on a left sidebar.
I have a PNLHome, PNLServerSettings, PNLScripts, and PNLSettings as the main panels.
Each of the Panels .Name properties are "PNLHome", "PNLServerSettings", ....etc.
I want to be able to change the "Start Up Screen" based on the selection you make in a ComboBox in PNLSettings, so when you launch the app, the Panel that is immediately showing is based on that ComboBox selection.
I have the ComboBox items in place and I have a function that hides all panels and shows the one you pass into it:
showPanel(PNLHome) for example which will essentially just show the Panel you give it, all this is working fine. This showPanel() is triggered upon clicking one of the main buttons on the left sidebar
What I want is to pass a Control.Name string into my showPanel() func, then I want to store this .Name string into My.Settings via a "Save/Apply" button in the Settings Panel which I can set up easily.
Since I will be passing the .Name String into my showPanel() function, I need to be able to reference the Panel I'm showing by it's name rather than the Object ID itself (I'm not sure if it's called an ID but it's the 'name' declaring the Panel via a "DIM WithEvents PNLHome As Panel" declaration.
To summarize the question; Can I reference the PNLHome by its .Name Property?
Otherwise can I store the ID of PNLHome directly in My.Settings instead? I could easily pass My.Settings.StartPanel into my showPanel() func.
Just use the Controls collection to find the control.
Private Function GetPanel(PanelName As String) As Panel
Dim SomePanel As Panel = CType(Controls(PanelName), Panel)
Return SomePanel
End Function

DevExpress Add items to ComboBox at run time

I've recently started using the DevExpress Comboboxes and other UI components. But I'm struggling to add items to the ComboBoxes during rune-time. The default Visual Basics's ComboBox worked simply by using the Items.Add code... But DevExpresses seam much more complicated, I've spent the last hour trying to work out how to add an item to the Combobox during run-time. And now this is my final option.
Has anyone got an idea on how this could be done? To be a little clearer, I'm trying something like this:
If combobox1.SelectectItem = "Item1" Then
Combobox2.items.add("Added Item String")
end if
just using syntax the DevExpress classes will 'understand'.
All DevExpress editors have the SomeEditor.Properties property which contains settings specific to the editors. So, you can get Items collection from comboBox.Properties.Items property:
If combobox1.SelectectItem.ToString = "Item1" Then
Combobox2.Properties.Items.Add("Added Item String")
End If

Visual Basic: How to replace selected NumericUpDowns with ComboBoxes

Is it possible to replace the selected NumericUpDown controls with ComboBoxes?
I understand that not all properties can be conserved but the only properties I need are the location and the size.
The workflow I have in mind is as follows:
Select certain NumericUpDowns
Click replace with... and then select ComboBoxes (or any other approach)
Where the NumericUpDowns were, there are now ComboBoxes of the same size
The reason I want to do this is that I have to put together a GUI with multiple tabs. Each tab page has a list of Labels with either NumericUpDowns or ControlBoxes next to it. The order of the controls changes per tab. I just want to copy the items on the first tab and paste them on the other tabs. Then per tab I only have to change certain NumericUpDowns into ComboBoxes.
I started with VB yesterday so I might be overlooking something.
The quickest is doing it manually, we cant change your GUI remotely - since you are at an entry level with a language you do not know well, RAD is the best recommendation - this way you can study what it does - just like learning HTML with DreamWeaver's RAD tools.
Since your interested, (I know your new to VB so I'dd make it heaps clear) you do these steps:
a) Open Winforms VS 2008 solution
b) Click the File > Create New Project > WinForms
c) Double click the form and it will show you the forms code
d) Then in the constructor method you will see the line InitializeComponent
e) Right Click on this method call and chose Goto Definition
f) This will show you the code that populates the form with controls
g) Then for each form I'm suggestion you replace all the NumericUpDown's with ComboBoxes in the xyz.Designer.vb file
However I'd really recommend doing it with Visual Studio IDE. Don't be frightened.
Private sub Replace_By_ComboBox(ByVal nud As NumericUpDowns)
'Create new combo box
Dim cbx As New ComboBox
cbx.Left = nud.Left
cbx.To = nud.Top
cbx.Width = nud.Width
cbx.Height = nud.Height
cbx.Visible = True
cbx.Enabled = True
'Add the combo box
nud.Parent.Controls.Add(cbx)
'Remove the NumericUpDowns
nud.Parent.Controls.Remove(nud)
End Sub

CheckedListBox VB.Net MultiExtended SelectionMode

I have a CheckedListBox with a few items and I want to allow user to select more than one using arrows keys or mouse clicks combined with shift and ctrl keys so I set SelectionMode property to MultiExtended.
In design time it is not possible I get an error:
value property is not valid.
and if I set it in runtime by doing:
clbEmployees.SelectionMode = SelectionMode.MultiSimple
I get an error too:
CheckedListBox is not compatible with multiple selection.
How could I do this?
It's not supported for the CheckedListBox.
However, I'm fairly sure that you could mimic that functionality in a ListView. Just look at the CheckBoxes and MultiSelect properties of the Listview. As far as I can tell from the documenation those are compatible.
This might be too late, but I just put my solution here; Works perfect for me:
1- Just leave the CheckedListBox Selection mode as "ONE' in property sheet.
2- in your code, loop thru the checked item in your checked Box using the checked item property as following:
For each XX as 'DataTpe' in CheckedListBox.CheckedItems
'Here you assign each checked item to wherever you want to direct to'
Next