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.
 

ritesh_jain1982

Well-known member
Joined
Jan 10, 2005
Messages
53
Programming Experience
1-3
Hi,
Try to use Select Method of ListBox as below........

ListBox1.Select()

ListBox1.Focus()

ListBox1.SelectedIndex = 3



I hopw this will help u..................

Regards,
Ritesh
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
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:
Top Bottom