Question How to retain focus for listbox after setting index

KAMPATEL1973

Member
Joined
Oct 7, 2015
Messages
11
Programming Experience
Beginner
Hi Guys,

I have some code as follows for a listbox that is in a windows form with an OK button:




What I am finding is that the item gets selected as I require, but the OK button on the form gets focus, ideally I would like the list box to retain focus even though I have selected an index for it to select.
Is there a way to do this?

VB.NET:
            Dim source() As String = Split(xml, ",")
            Dim i As Integer
            For i = 0 To UBound(source)
                DialogBoxListBox.SelectList2.Items.Add(source(i))
            Next
            DialogBoxListBox.SelectList2.SelectedIndex = 0

Everything works ok, its just the focus part that's not working.

Any help appreciated...
 
There is only one form in the whole project and it has a list box on it with an OK button. I am looking at changing the tab index to see if this helps.

Thanks
 
I am looking at changing the tab index to see if this helps.
That won't help because the listbox is not an input control, so the button will have initial focus no matter the tabindex order. You can select the listbox by using its Select method:
SelectList2.Select()
 
Using TabIndex really messed my forms up :-(. Its changed to 1 0.0 0.1 etc.

However using the method you have stated John. this has done what i wanted. Thanks
 
Back
Top