Search code examples
vb.netora-12154

Adding to a ComboBox in Visual basic 2010


I want the user of the program to be able to add a High School name to an already populated ComboBox. I added 4 different High Schools to the ComboBox at design time. I have a MenuStrip with an Edit tool and Add High School tool. Here is the code I have written:

Private Sub AddHighSchoolToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddHighSchoolToolStripMenuItem.Click
    Me.ComboBox1.Items.Add("")
End Sub

When I run the program, the 4 High School appear, when I "Add High School" I get a blank space. What am I doing wrong?


Solution

  • That's because you are entering an empty string to the ComboBox.

    Try this instead,

    Dim schName As String
    
        schName = InputBox("Enter your High School name", "High School name")
    
        If Not schName = "" Then
    
            ComboBox1.Items.Add(schName)
    
        End If