Listbox question?

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Hi there, I am using 5 listboxes each having 3 items in them. when the user selects an item in the listbox I want a have a number appear in a textbox next to it. I will then need the numbers for a math function - this part I can handle. What is the syntax for giving the items a value and showing it in the textbox?

Thanks
 
No idea what number you're wanting to put in the textbox. You should easily be able to adapt this to your needs though.

VB.NET:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
    TextBox1.Text = ListBox1.SelectedIndex + 1
End Sub
 
Thanks for the reply, there are 3 diff items in the listbox and they each have a diff number that I want to assign them, how do I differ b/t the items and them each there own value.

item1 = 1
item2 = 2
item3 = 3
etc...

sorry I am very new...but working hard!
 
As mentioned above, you could use the index of the items.

So, if the index is 0 (in this case item1) then set the value in the textbox.

VB.NET:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        MessageBox.Show(ListBox1.SelectedIndex)
    End Sub

When clicking the listbox, you'll get the index, you could then base the value in the textbox using the index.

(Unless I have the wrong end of the stick of what you want to achieve here).
 
yes, but I do not know the correct syntax for assigning the value I want to the propective indexes, like index0 will = 2, index1 will = 1, and index3 will = 0. And these numbers to be placed in the textbox.

Thanks again.
 
Try something like this...

Case ListBox1.SelectedIndex

VB.NET:
        Select Case ListBox1.SelectedIndex
            Case 0
                TextBox1.Text = "This is item 1"
            Case 1
                TextBox1.Text = "This is item 2"
            Case 2
                TextBox1.Text = "This is item 3"
        End Select
 
exactly what I needed, forgot about select case. Amatuer mistake, it works great now, many thanks.
 
Back
Top