Controls: List Boxes


Methods: 

  • listbox.Items.Clear -- Clears the list box. 
  • listbox.Items.Insert or listbox.Items.Add -- Adds an item to the list box.  ex: listBox1.Items.Add("Item " & x.ToString())
  • listbox.Items.Count -- Returns the number of items in the list box. 
  • listbox.Items.Remove (index) -- Deletes item at index 
  • listbox.Items (index) -- Returns the item at index 
  • if index = lstBox.SelectedIndex -- Indicates (if true) that the item at index has been selected 

Properties:

  • lstBox.Text -- Returns the selected item in the list box
  • lstBox.SelectedIndex -- Sets or returns the index of the currently selected item in the control 

Populating a List Box at Design Time:

To populate a list box at design time, select the list box by clicking on it, then select the Items property in the Properties window.  Click the ellipses, and begin typing the list items, ending each line with an Enter.  When you have completed your entries simply click the Okay button. 


Tricks

To force the list box to display (move focus to) the most recent addition to the list box, you need the index of the last item added.  You can obtain this from the value returned by Items.Add For example,

Dim newIndex As Integer
newIndex = lstFred.Items.Add(15)

To highlight that most recent addition, use the setSelected method.  For example,

lstFred.SetSelected(newIndex, True)

 

To synchronize two list boxes in order to display related elements (such as student name in one box and grade in another), see this link.

To reset a list box so that entries are no longer highlighted, first set the list index to 0 and then reset it to -1.

lstBox.SetSelected(0, True)
lstBox.SetSelected(-1, True)

maybe okay

ListBox Control Changes in Visual Basic .NET