help with listbox selection

Chaseshaw

Member
Joined
Apr 21, 2010
Messages
8
Programming Experience
Beginner
I have code to create a listbox and populate it, and would like the box to behave like it does when clicked, e.g. if you CLICK on "a" you see it blue and can press the down arrow to move down to "b". how do I achieve this programmatically?

Private Sub junkloader()
brandlist.Visible = True
brandlist.Items.Clear()
brandlist.Items.Add("a")
brandlist.Items.Add("b")
brandlist.Items.Add("c")
brandlist.Items(0).Selected = True
brandlist.Focus()
End Sub

this code SELECTS it properly, but I want to be able to navigate with the up and down arrows. how do I do this? thanks.
 
to move down (be sure not to go past the end of the list)
brandlist.SelectedIndex += 1

to move up (be sure not to below item 0)
brandlist.SelectedIndex -= 1
 
Thanks for the suggestion. it says "'SelectedIndex' is not a member of 'System.Windows.Forms.ListView'." how do I fix this? thanks.
 
Your thread title says "listbox". A ListBox and a ListView are two completely different things. It's important to use the proper names for things to avoid confusion.

As for the problem, I'm not sure there is one. I just did a quick test and using the arrow keys changed the selection in a ListView for me.
 
you are completely right I didn't notice that before! I am using a listview. how do I change the selection in that? (sorry I'm still relatively new at vb.net)
 
You already know how because you're already doing it in the code you posted earlier. Like I said though, using the arrow keys changed the selection automatically for me
 
Back
Top