Results 1 to 5 of 5

Thread: get text from list box?????

  1. #1

    Thread Starter
    Lively Member cargobay69's Avatar
    Join Date
    Nov 2001
    Location
    Kessel Prison Camp
    Posts
    97

    get text from list box?????

    OK, I'm feeling really stupid. I'm using the VB.net ListBox control and I want to get the text of selected items listed in the ListBox, but I can't figure out which property or function can do that (looking for equililant to ListBox.List(Index) in VB 6.0) I've tried ListBox.GetItemText - doesn't work. Also tried ListBox.Items.[Everything] - doesn't work. And I've tried a number of other things including Help. Anybody know how I can do this?

    Many Thanks...
    Darrin@CB69
    -----------------------------------------------
    Arrogance kills brain cells
    -----------------------------------------------
    Private Sub Sandwich (big As Byte)
    On Error GoTo Pub

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Took me a while to figure out too:

    here is the code

    Code:
    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            MessageBox.Show(Convert.ToString(ListBox1.SelectedItem))
    End Sub
    Dont gain the world and lose your soul

  3. #3
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    VB Code:
    1. Dim intX As Int32
    2.       For intX = 0 To ListBox1.Items.Count - 1
    3.          MsgBox(ListBox1.Items.Item(intX), MsgBoxStyle.Information)
    4.       Next

  4. #4

    Thread Starter
    Lively Member cargobay69's Avatar
    Join Date
    Nov 2001
    Location
    Kessel Prison Camp
    Posts
    97
    Thanks guys. Strange they made that one so abstract.
    Darrin@CB69
    -----------------------------------------------
    Arrogance kills brain cells
    -----------------------------------------------
    Private Sub Sandwich (big As Byte)
    On Error GoTo Pub

  5. #5
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926
    I think this is because a listbox can now contain any object that has a ToString method.

    If I did understand this correctly:
    Because the Object class itselve provides a ToString method, and every object inherits from the Object class, the only objects that do not qualify would be the ones that override the ToString method PRIVATE.
    Of course not every object would give the desired results, because the return value of the ToString method will be displayed in the listbox.

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