none
getting index/value from listbox RRS feed

  • Question

  • fairly simple! or so i thought, i would like to grab the value/index from a listbox i have populated, tried under the click event / index changed:

    Protected Sub listTeam_SelectedIndexChanged(sender As Object, e As EventArgs) Handles listTeam.SelectedIndexChanged
            Dim selectedTeam As String
            selectedTeam = listTeam.Items(listTeam.SelectedIndex).ToString
            TextBox1.Text = selectedTeam
        End Sub

    my listbox is called listTeam, has a bunch of teams in it, all i want is to be able to grab the value i have assigned each item in the list box, place this value into a textbox and assigned index, any ideas?...
    Friday, April 20, 2012 2:15 PM

Answers

  • now sorted, rookie mistake i had used the index changed but this was the problem, now placed this code in the page load:

            If listTeam.SelectedValue <> "" Then
                selectedTeam = listTeam.SelectedValue
                TextBox1.Text = selectedTeam
            End If

    • Marked as answer by crazy_legs Friday, April 20, 2012 2:32 PM
    Friday, April 20, 2012 2:32 PM

All replies

  • err is:

    Index was out of range. Must be non-negative and less than the size of the collection.

    Friday, April 20, 2012 2:16 PM
  • now sorted, rookie mistake i had used the index changed but this was the problem, now placed this code in the page load:

            If listTeam.SelectedValue <> "" Then
                selectedTeam = listTeam.SelectedValue
                TextBox1.Text = selectedTeam
            End If

    • Marked as answer by crazy_legs Friday, April 20, 2012 2:32 PM
    Friday, April 20, 2012 2:32 PM