954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

listbox help

i am having trouble linking 2 listboxes together... What i woud like to happen is a selecting from listbox1 to populate listbox2. Any ideas on what to do?

thanks in advance

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

Maybe this small example gives you an idea:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		For i As Integer = 1 To 10
			ListBox1.Items.Add("Value " & i)
		Next
	End Sub

	Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
		ListBox2.Items.Clear()
		For i As Integer = 40 To 50
			ListBox2.Items.Add(ListBox1.SelectedItem.ToString & " => " & New String(Chr(i), 5))
		Next
	End Sub
GeekByChoiCe
Master Poster
721 posts since Jun 2009
Reputation Points: 208
Solved Threads: 168
 

See if this helps.:)

Public Class Form1
    Private arCmbItems() As String = {"item 1", "item 2", "item etc.", "item.Else"}

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With ListBox1
            .Items.AddRange(arCmbItems) '// add items to ListBox from String.Array.
        End With
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        With ListBox2.Items
            If Not ListBox1.SelectedIndex = -1 Then '// check if item is selected since Index.of -1 is No.Item.Selected.
                .Clear() '// .Clear for new input.
                Select Case ListBox1.SelectedItem '// check value selected.
                    Case arCmbItems(0) '// case 1st.item.
                        .Add("1a") : .Add("1b") : .Add("1c")
                    Case arCmbItems(2) '// case 3rd.item.
                        .Add("etc.a") : .Add("etc.b") : .Add("etc.c")
                    Case arCmbItems(1) '// case 2nd.item.
                        .Add("2a") : .Add("2b") : .Add("2c")
                    Case Else '// if any other item selected.
                        .Add("No items")
                        .Add("in String.Array")
                        .Add("for this .SelectedItem")
                End Select
            End If
        End With
    End Sub
End Class
codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

thank you. The code helped a lot

ng5
Light Poster
41 posts since Dec 2011
Reputation Points: 19
Solved Threads: 0
 

>>The code helped a lot
The "code" seems to help quite a bit more than a lot.
Glad I could help.:)
code.order

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

Hi...I have this assignment wherein the user will input 100 names in inputbox then display the first ten(10) names in the list box...I dont know how to apply the array and loops in it..I find it so hard..I need your help everyone..I wish someone could.

april_004
Newbie Poster
1 post since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

april_004, start your own thread and send me a link via a p.m.; will see what I can do Then.
ng5, my.apologies for this post.

codeorder
Posting Virtuoso
1,913 posts since Aug 2010
Reputation Points: 255
Solved Threads: 384
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: