setting a listbox's selection

  • Thread starter Thread starter Bernie Yaeger
  • Start date Start date
B

Bernie Yaeger

I want to open a window with a listbox (custlist) in it, but I want to
automatically select a certain item in the listbox, and have it be the
correct selectedindex. The call to open this window knows what it seeks,
which is variable. So one time I may wish to select item 45 (marsdon) and
sometimes item 1997 (albert). How can I do this?

Thanks for any help.

Bernie Yaeger
 
August 7, 2004

You can use custlist.SelectedIndex = index or
custlist.SelectedItem =
String. The following is an example of selecting a item based on a
random
number. The listbox1.items are: Test1, Test2, Test3, ..., Test6.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Randomize()
Dim r As Integer
r = Int(Rnd() * 5)
Label1.Text = r 'Displays number for testing purposes
ListBox1.SelectedItem = "Test" & r ' OR
Listbox1.SelectedIndex = r - 1 ' - 1 because of zero based array
End Sub

These methods work fine for your purpose. Have a nice day!


Joseph MCP
 
Hi Joe,

I actually found another way of getting this done, but tx - you suggestion
worked fine as well!

Thanks again,

Bernie
 
Back
Top