Visual Basic .NET - ComboBox Control
The VB.NET ComboBox control is used to provide a drop-down list of options for the user to select from.
Common Properties:
- Items: Gets or sets the items in the ComboBox
- SelectedIndex: Gets or sets the index of the currently selected item
- Text: Gets or sets the text of the currently selected item
Common Methods:
- AddItem: Adds an item to the ComboBox
- RemoveItem: Removes an item from the ComboBox
- Clear: Removes all items from the ComboBox
Common Events:
- SelectedIndexChanged: Raised when the user selects a different item in the ComboBox
- TextChanged: Raised when the user changes the text of the currently selected item
- DropDown: Raised when the drop-down list is shown
Example:
Here is a simple example of using the ComboBox control in VB.NET:
Create a new Windows Forms Application in Visual Studio.
Drag a ComboBox control onto the form.
In the form's Load event, add the following code to populate the ComboBox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Option 1")
ComboBox1.Items.Add("Option 2")
ComboBox1.Items.Add("Option 3")
End Sub
Add a button to the form and handle its Click event with the following code to display the selected item in a message box:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("You selected " & ComboBox1.SelectedItem.ToString())
End Sub
Run the application and select an item from the ComboBox, then click the button to see the selected item in a message box.
This is just a simple example, but the ComboBox control can be used in many ways to provide a user-friendly interface for selecting from a list of options.