Help with Focus on ListBox

propagandhi

Member
Joined
Feb 20, 2005
Messages
18
Programming Experience
Beginner
First of all, thanks for your time. My problem is that when i load my form, i need to have the focus set to a string in one of my list box's.
 
if you know what the item index is then simply:

Listbox1.SelectedIndex = 1 '1 being the number of the index of the item that needs to be selected

if you know what the string is but not the index just use a quick loop:

VB.NET:
Dim strName as String 'String that needs to be found
Dim intCounter as Integer
Dim intRecordNumber as Integer = -1
For intCounter = 0 To lstNames.Items.Count - 1
  lstNames.SelectedIndex = intCounter
  If lstNames.Text = strName Then intRecordNumber = intCounter
Next intCounter
lstNames.SelectedIndex = -1
lstNames.SelectedIndex = intRecordNumber
 
Last edited:
Back
Top