Results 1 to 6 of 6

Thread: Selected item index on a listbox...

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    60

    Exclamation Selected item index on a listbox...

    Hi every1 !
    I have a listbox and an array.
    I want to link every listbox item to an array value, so, when I double click an item I want to retrieve its Index ( 1,2,3.....) like on VB6... but
    listbox.selecteditem returns the string, not the position...

    I cant find a command lie .item.index....
    help ?
    Thanks !!
    Martin

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Re: Selected item index on a listbox...

    Is this what you want ?
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim arry() As String = {"item1", "item2", "item3"}
    3.         'bind the array to the listbox
    4.         ListBox1.Items.AddRange(arry)
    5.     End Sub
    6.  
    7.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    8.         'show the index of selected item
    9.         MessageBox.Show(ListBox1.SelectedIndex)
    10.     End Sub
    11. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    60

    Exclamation I really dont know...

    selecteditems is returning -1 ....
    I can add items using:

    listbox.items.add "string"

    and when I double click an item it fires doubleclick event where i put the msgbox with ....selecteditem.
    showing -1.

    that is my doubt...

    Thanks !!
    Martin

  4. #4
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I can't understand what problem you are in now . I've tried it here and worked fine . You might need to try
    VB Code:
    1. Listbox1.SelectedItem.ToString()

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2002
    Posts
    60

    Exclamation still...

    ok...
    SelectedItem.ToString() is returning me the text item, not the "position" of the item I have selected....

    Does that help ?
    Do you remember in VB6 that I could retrieve the index of an item, for example if I click the 4th item on the list, itemindex would return 4. not the text.
    Martin

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    What about the first code I posted . It does what you want I guess . Like this :

    VB Code:
    1. MessageBox.Show(ListBox1.[B]SelectedIndex[/B] )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width