List Boxes (VB.NET)



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. 



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):

Private Sub lstBox1_Scroll( )
       lstBox2.TopIndex = lstBox1.TopIndex
End Sub 

Private Sub lstBox2_Scroll( )
       lstBox1.TopIndex = lstBox2.TopIndex
End Sub

maybe okay--see 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