List box features

S

Steve

Hello all:

I am working on a project that has two list boxes side-by-side on a
form. What I want to happen is when a user clicks on an item in the
list box on the left, the list box on the right displays information
related to only that item. I would like to be able to connect to a
SQL database to retrieve the items and possibly store them in an array
and then loop through the array to populate the list box.

ListBox1 ListBox2
--------------- ----------------
Sports ----> Basketball
Soccer
Football
Cars -------> Ford
Chevrolet
Dodge

Can someone help me on this or can someone point in the direction to
at least get things moving?

Thanks for the info.

Steve
 
R

Reg Verrin

Set up an event handler for Listbox1 so that you know the item that's
been clicked.


Private Sub Listbox1_MouseClick (etc)
Dim ItemNumber As Integer

ItemNumber = Listbox1.SelectedIndex

'ItemNumber now holds the index of the item that was clicked
(starts at zero)


End Sub


You can then provide details for that item in Listbox2.
 
S

Steve

Set up an event handler for Listbox1 so that you know the item that's
been clicked.

Private Sub Listbox1_MouseClick (etc)
Dim ItemNumber As Integer

ItemNumber = Listbox1.SelectedIndex

'ItemNumber now holds the index of the item that was clicked
(starts at zero)

End Sub

You can then provide details for that item in Listbox2.

Thank you very much. I will try this out.
 
K

kimiraikkonen

Hello all:

I am working on a project that has two list boxes side-by-side on a
form. What I want to happen is when a user clicks on an item in the
list box on the left, the list box on the right displays information
related to only that item. I would like to be able to connect to a
SQL database to retrieve the items and possibly store them in an array
and then loop through the array to populate the list box.

ListBox1 ListBox2
--------------- ----------------
Sports ----> Basketball
Soccer
Football
Cars -------> Ford
Chevrolet
Dodge

Can someone help me on this or can someone point in the direction to
at least get things moving?

Thanks for the info.

Steve

Hi,

If i understood right, here may be the one that demonstrates what you
meant:

Just add 2 listbox's on your form named "ListBox1" which resides on
the right and "ListBox2" which resides on the left of your form:

' ----Code Begins----

'Full code will look like this:

Public Class Form1

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem = "Sports" Then
'First clear listbox2
ListBox2.Items.Clear()
'Then display add (display) related
'items in listbox2
ListBox2.Items.Add("Basketball")
ListBox2.Items.Add("Soccer")
ListBox2.Items.Add("Football")
ElseIf ListBox1.SelectedItem = "Cars" Then
'First clear listbox2
ListBox2.Items.Clear()
'Then display add (display) related
'items in listbox2
ListBox2.Items.Add("Ford")
ListBox2.Items.Add("Chevrolet")
ListBox2.Items.Add("Dodge")
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Here items are added manually, as you can retrieve them
'using your proper SQL connection methods
ListBox1.Items.Add("Sports")
ListBox1.Items.Add("Cars")
End Sub
End Class

' ---Code Ends------


Hope this helps,

Onur Güzel
 
K

kimiraikkonen

Hi,

If i understood right, here may be the one that demonstrates what you
meant:

Just add 2 listbox's on your form named "ListBox1" which resides on
the right and "ListBox2" which resides on the left of your form:

' ----Code Begins----

'Full code will look like this:

Public Class Form1

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem = "Sports" Then
'First clear listbox2
ListBox2.Items.Clear()
'Then display add (display) related
'items in listbox2
ListBox2.Items.Add("Basketball")
ListBox2.Items.Add("Soccer")
ListBox2.Items.Add("Football")
ElseIf ListBox1.SelectedItem = "Cars" Then
'First clear listbox2
ListBox2.Items.Clear()
'Then display add (display) related
'items in listbox2
ListBox2.Items.Add("Ford")
ListBox2.Items.Add("Chevrolet")
ListBox2.Items.Add("Dodge")
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Here items are added manually, as you can retrieve them
'using your proper SQL connection methods
ListBox1.Items.Add("Sports")
ListBox1.Items.Add("Cars")
End Sub
End Class

' ---Code Ends------

Hope this helps,

Onur Güzel

Sorry i want to correct positions of listboxes on your form,
"ListBox1" should be placed on the LEFT of your form and "ListBox2"
should be placed on the RIGHT of your form, the rest code is the same
as my previous post.

Hope this helps,

Onur Güzel
 
S

Steve

Sorry i want to correct positions of listboxes on your form,
"ListBox1" should be placed on the LEFT of your form and "ListBox2"
should be placed on the RIGHT of your form, the rest code is the same
as my previous post.

Hope this helps,

Onur Güzel

Thank you very much for your help.

Steve
 
S

Steve

Hi,

If i understood right, here may be the one that demonstrates what you
meant:

Just add 2 listbox's on your form named "ListBox1" which resides on
the right and "ListBox2" which resides on the left of your form:

' ----Code Begins----

'Full code will look like this:

Public Class Form1

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
If ListBox1.SelectedItem = "Sports" Then
'First clear listbox2
ListBox2.Items.Clear()
'Then display add (display) related
'items in listbox2
ListBox2.Items.Add("Basketball")
ListBox2.Items.Add("Soccer")
ListBox2.Items.Add("Football")
ElseIf ListBox1.SelectedItem = "Cars" Then
'First clear listbox2
ListBox2.Items.Clear()
'Then display add (display) related
'items in listbox2
ListBox2.Items.Add("Ford")
ListBox2.Items.Add("Chevrolet")
ListBox2.Items.Add("Dodge")
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Here items are added manually, as you can retrieve them
'using your proper SQL connection methods
ListBox1.Items.Add("Sports")
ListBox1.Items.Add("Cars")
End Sub
End Class

' ---Code Ends------

Hope this helps,

Onur Güzel

Thanks for the help. Looking at your example started to generate
ideas for what I wanted to do. I was able to get the listbox working
the way I wanted it to.
 
K

kimiraikkonen

Thanks for the help. Looking at your example started to generate
ideas for what I wanted to do. I was able to get the listbox working
the way I wanted it to.

No problem, i'm glad that it worked,

Thanks,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top