lmoe23 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.
Answering (and following ): here is a concise, practical how-to for populating a ComboBox, selecting an index, and using a Select Case or If/Else to react. Examples for both VB.NET (WinForms) and classic VB are included, plus a few tips you can apply right away.
' VB.NET - populate and react
ComboBox1.Items.Add("Red")
ComboBox1.Items.Add("Green")
ComboBox1.Items.Add("Blue")
ComboBox1.SelectedIndex = 0 ' selects first item
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim idx As Integer = ComboBox1.SelectedIndex
If idx < 0 Then Exit Sub
Select Case idx
Case 0
' Red selected
Case 1
' Green selected
Case 2
' Blue selected
End Select
End Sub ' VB6 - populate and react
Combo1.AddItem "Red"
Combo1.AddItem "Green"
Combo1.AddItem "Blue"
Combo1.ListIndex = 0 ' selects first item
Private Sub Combo1_Click()
Dim i As Integer
i = Combo1.ListIndex
If i < 0 Then Exit Sub
Select Case i
Case 0
' Red selected
Case 1
' Green selected
Case 2
' Blue selected
End Select
End Sub Tips and cautions: SelectedIndex / ListIndex are 0-based and are -1 when nothing is chosen, so always check for < 0 before using. To set an index by value, find it first (VB.NET: Items.IndexOf("text")) and only assign if it is >= 0. If the ComboBox is data-bound, read SelectedValue or the bound object instead of Items. If the box is editable, Text may contain a typed value even when SelectedIndex is -1. For reference on the WinForms property behavior see ComboBox.SelectedIndex.
What do you want to do with a combo box?
What do you want to do with a combo box?
how to do a select case statement or if else statement in a combo box?
first how to declare a the name in the combo box and give in a index..
thanks.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.