Results 1 to 4 of 4

Thread: How do I display everything in a listbox with a msgbox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2003
    Location
    San Jose, CA
    Posts
    3

    Question How do I display everything in a listbox with a msgbox?

    I'm using Visual Basics.NET 2002.
    My form has 1 ComboBox, 1 ListBox and 3 Buttons.

    Private Sub form_load...
    ComboBox.Items.Add("Cherry")
    ComboBox.Items.Add("Grape")
    ComboBox.Items.Add("Mango")
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged...
    ListBox.Items.Add(ComboBox.SelectedItem)
    End Sub

    Private Sub ButtonClearList_Click
    ListBox.Items.Clear
    End Sub

    Private Sub ButtonRemoveFruit_Click
    If ListBox.SelectedIndex = -1 Then
    MsgBox("No fruit was choosen.")
    Else
    Dim I As Integer = ListBox.SelectedIndex
    ListBox.Items.RemoveAt(I)
    End If
    End Sub

    Private Sub ButtonShowAllMyFruits_Click...
    'After I picked, 1 Cherry, 2 Grapes and 4 Mangos from the ComboBox, _
    'my chosen fruits will appear in the ListBox.
    'How do I show all my choices in a message box?
    'I want the message box to say "You picked 1 cherry, 2 grapes and 4 mangos.")
    End Sub

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Declare three variables and store in them selected items from comboboxes and listbox accordingly . then just use this "

    messagebox.show(vari1 & " , " & vari2 & " , " & vari3 & " , "

    or follow the formula you like .

  3. #3
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    I wasn't sure from your description what was in the listbox??

    You can loop through the contents of the listbox like this:

    Dim o As Object
    Dim str As String = ""
    For Each o In ListBox1.Items
    str += o.ToString
    Next
    MsgBox(str)

    Inside the loop you could count them, or do whatever.

    HTH

    G

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Originally posted by ggprogram
    I wasn't sure from your description what was in the listbox??

    You can loop through the contents of the listbox like this:

    Dim o As Object
    Dim str As String = ""
    For Each o In ListBox1.Items
    str += o.ToString
    Next
    MsgBox(str)

    Inside the loop you could count them, or do whatever.

    HTH

    G
    Use a string builder object though, it will be slightly faster. System.Text namespace I do believe.

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