Display comboBoxList in MessageBox

Joined
Dec 10, 2007
Messages
18
Location
Florida
Programming Experience
Beginner
Hello all,
I am trying to display the contents of my comboBoxList in a messageBox. I cant seem to grasp on how to do this. I know I have to use a loop but, well this is what I have came up with so far. Any help would be greatly appreciated!

VB.NET:
Private Sub exampleListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exampleListToolStripMenuItem.Click 


Dim messageString As String

Dim indexInteger As Integer

Dim maximumInteger As Integer

maximumInteger = Me.exampleComboBox.Items.Count - 1

messageString = "Movies currently in list: "

MessageBox.Show(messageString, "Movie categories" _

& me.exampleComboBox.

For indexInteger = 0 To maximumInteger

    Me.exampleComboBox.Items(indexInteger)

Next indexInteger

End Sub
 
Last edited by a moderator:
Hello,

It is not clear that whether you wish to display each item in the combo box
in a message box individually or do you wish to create a single list of all the
items of the combo box and then display them in a message box.

You can try either of the codes:

VB.NET:
Dim str As String = ""
For i = 0 To Me.ComboBox1.Items.Count - 1

           str = str & Me.ComboBox1.Items(i) & ControlChars.NewLine
Next
MsgBox(str)

 or

Dim str As String = ""
For i = 0 To Me.ComboBox1.Items.Count - 1

            msgbox( Me.ComboBox1.Items(i) )
Next

I hope this helps,

Allen

Allen Smith
Software Engineer
ComponentOne LLC
www.ComponentOne.com
 
VB.NET:
Dim upperBound As Integer = Me.ComboBox1.Items.Count - 1
Dim items(upperBound) As String

For index As Integer = 0 To upperBound Step 1
    items(index) = Me.ComboBox1.GetItemText(Me.ComboBox1.Items(index))
Next index

MessageBox.Show(String.Join(Environment.NewLine, _
                            items))
 
Elegant solution jmcilhnney. indeed.

Also you can do that by using generic list e.g.
VB.NET:
Dim genericList As New List(Of String)

For i As Integer = 0 To Me.ComboBox1.Items.Count - 1
       genericList.Add(Me.ComboBox1.Items(i).ToString)
Next

MessageBox.Show(String.Join(Environment.NewLine, _
                                     genericList.ToArray))

the code above will do the same :)
 
Elegant solution jmcilhnney. indeed.

Also you can do that by using generic list e.g.
VB.NET:
Dim genericList As New List(Of String)

For i As Integer = 0 To Me.ComboBox1.Items.Count - 1
       genericList.Add(Me.ComboBox1.Items(i).ToString)
Next

MessageBox.Show(String.Join(Environment.NewLine, _
                                     genericList.ToArray))

the code above will do the same :)
That's not going to work in every case. If the ComboBox was bound to a DataTable then you'd get "System.Data.DataRowView" repeated over and over. That's why I used GetItemText. It will get the text displayed in the control for that item no matter what type the items are.
 
Actually i just wanted to point out possible generic list implementation.
Means you can still use the GetItemText method with the generic list.
However good point. :)
 
Success

THank you guys for your help. I was able to get it to work. I have one question though Why do I have to implement the code in this manner:?
VB.NET:
strString = strString & Me.genericComboBox.Items(indexInteger) & ControlChars.NewLine & ControlChars.Tab
[CODE]
 Instead of just doing
[CODE]strString = Me.genericComboBox.Items(indexInteger) & ControlChars.NewLine & ControlChars.Tab
Leaving out that intial strString= strString? Why cant I do it like that? I dont understand the purpose of doing strString = strString before adding the other code. Can someone please tell me? Thanks
Below is the full code of my accomplishment

VB.NET:
        Dim messageString As String
        Dim strString As String = ControlChars.Tab '""
        Dim indexInteger As Integer
        Dim maximumInteger As Integer
        maximumInteger = Me.genericComboBox.Items.Count - 1

For indexInteger = 0 To maximumInteger
            strString = strString & Me.genericComboBox.Items(indexInteger) & ControlChars.NewLine & ControlChars.Tab
        Next
        MessageBox.Show(strString, messageString, MessageBoxButtons.OK, MessageBoxIcon.Information)
 
Let's say that you had three numbers and you wanted to add them up:
VB.NET:
sum = 10
sum = 20
sum = 30
What's the value of 'sum' after that? Is it 60, or is it 30?
VB.NET:
sum = 10
sum = sum + 20
sum = sum + 30
Now what's the value of sum after that?

Now ask yourself again why you need to concatenate the existing value of the string along with the value you want to append.
 
Back
Top