Question : Getting data from two ListBoxes with a button click event

I am using 2 ListBoxes (playersList) and (trainersList) to display the names of players and trainers retrieved from one table in an an Access database. The first ListBox contains the players names and the corresponding second ListBox contains the trainers names with the index of the items in the first ListBox being the same as the index of the items in the second.


Presently I am using one button to get the selected text from the players list into the player label with the following code:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
       
        For Each item As ListItem In subjectList.Items
            If (item.Selected) Then
                lblplayerName.Text = item.Text
                End If
        Next
    End Sub

What I need is for this button to select both player and the corresponding trainer from the two lists and place them in two labels. The player name into the playerLabel and the trainer name into the trainerLabel.

I guess the code should first establish the selected index of the buton_click event and use it to select the items in both lists.
My problem is that I don't know how to that.


Answer : Getting data from two ListBoxes with a button click event

Didn't understand exactly how your two LBs are linked...
But you can get the index of first selected item and use it to get second item:
for (int i = 0; i < ListBox1.Items.Count; i++)
        {
            if (ListBox1.Items[i].Selected)
            {
                lbltrainer.Text = ListBox2.Items[i].Value;
            }
        }
Let me know if I misunderstood.
Random Solutions  
 
programming4us programming4us