What is ComboBox Control in VB.net?
The ComboBox Control in VB.net is used to show multiple options in a drop-down menu.
It combines a listbox and a textbox, and the user can enter only one item.
A user may also choose an item from a drop-down list using this feature.
Let’s create a combo box by dragging a ComboBox Control from the Toolbox and dropping it on the form.
You can populate the list box items either from the properties window or at runtime.
To add items to a ComboBox, select the ComboBox control and go to the properties window for the properties of this control.
Click the ellipses (…) button next to the Items property. This opens the String Collection Editor dialog box, where you can enter the values one at a line.
Properties of the ComboBox Control in VB.net
The following are some commonly used Properties of the ComboBox Control in VB.net.
|#||Properties of the ComboBox Control in VB.net||Description|
|1.||AllowSelection||Gets a value indicating whether the list enables selection of list items.|
|2.||AutoCompleteCustomSource||Gets or sets a custom System.Collections .Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.|
|3.||AutoCompleteMode||Gets or sets an option that controls how automatic completion works for the ComboBox.|
|4.||AutoCompleteSource||Gets or sets a value specifying the source of complete strings used for automatic completion.|
|5.||DataBindings||Gets the data bindings for the control.|
|6.||DataManager||Gets the CurrencyManager associated with this control.|
|7.||DataSource||Gets or sets the data source for this ComboBox.|
|8.||DropDownHeight||Gets or sets the height in pixels of the drop-down portion of the ComboBox.|
|9.||DropDownStyle||Gets or sets a value specifying the style of the combo box.|
|10.||DropDownWidth||Gets or sets the width of the of the drop-down portion of a combo box.|
|11.||DroppedDown||Gets or sets a value indicating whether the combo box is displaying its drop-down portion.|
|12.||FlatStyle||Gets or sets the appearance of the ComboBox.|
|13.||ItemHeight||Gets or sets the height of an item in the combo box.|
|14.||Items||Gets an object representing the collection of the items contained in this ComboBox.|
|15.||MaxDropDownItems||Gets or sets the maximum number of items to be displayed in the drop-down part of the combo box.|
|16.||MaxLength||Gets or sets the maximum number of characters a user can enter in the editable area of the combo box.|
|17.||SelectedIndex||Gets or sets the index specifying the currently selected item.|
|18.||SelectedItem||Gets or sets currently selected item in the ComboBox.|
|19.||SelectedText||Gets or sets the text that is selected in the editable portion of a ComboBox.|
|20.||SelectedValue||Gets or sets the value of the member property specified by the ValueMember property.|
|21.||SelectionLength||Gets or sets the number of characters selected in the editable portion of the combo box.|
|22.||SelectionStart||Gets or sets the starting index of text selected in the combo box.|
|23.||Sorted||Gets or sets a value indicating whether the items in the combo box are sorted.|
|24.||Text||Gets or sets the text associated with this control.|
Methods of the ComboBox Control in VB.net
The following are some of the commonly used Methods of ComboBox Control in VB.net.
|#||Methods of the ComboBox Control in VB.net||Description|
|1.||BeginUpdate||Prevents the control from drawing until the EndUpdate method is called, while items are added to the combo box one at a time.|
|2.||EndUpdate||Resumes drawing of a combo box, after it was turned off by the BeginUpdate method.|
|3.||FindString||Finds the first item in the combo box that starts with the string specified as an argument.|
|4.||FindStringExact||Finds the first item in the combo box that exactly matches the specified string.|
|5.||SelectAll||Selects all the text in the editable area of the combo box.|
Events of the ComboBox Control in VB.net
The following are some of the commonly used Events of the ComboBox Control in VB.net.
|#||Events of the ComboBox Control in VB.net||Description|
|1.||FontChanged||It occurs when the property of the font value is changed.|
|2.||Format||When the data is bound with a combo box control, a format event is called.|
|3.||SelectIndexChanged||It occurs when the property value of SelectIndexChanged is changed.|
|4.||HelpRequested||When the user requests for help in control, the HelpRequested event is called.|
|5.||Leave||It occurs when the user leaves the focus on the ComboBox Control.|
|6.||MarginChanged||It occurs when the property of margin is changed in the ComboBox control.|
Let’s create a program to display the Calendar in the VB.NET Windows Form.
Public Class Form1 Dim DT As String Dim MM As String Dim YY As Integer Private Sub Button1_Click(sender As Object, e As EventArgs) End Sub Private Sub Button1_Click_1(sender As Object, e As EventArgs) End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load cb1.Items.Add("Date") cb1.Items.Add("1") cb1.Items.Add("2") cb1.Items.Add("3") cb1.Items.Add("4") cb1.Items.Add("5") cb1.Items.Add("6") cb1.Items.Add("7") cb1.Items.Add("8") cb1.Items.Add("9") cb2.Items.Add("Month") cb2.Items.Add("January") cb2.Items.Add("February") cb2.Items.Add("March") cb2.Items.Add("May") cb2.Items.Add("June") cb2.Items.Add("July") cb3.Items.Add("Year") cb3.Items.Add("2022") cb3.Items.Add("2023") cb3.Items.Add("2024") cb3.Items.Add("2025") cb3.Items.Add("2026") End Sub Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click DT = cb1.Text MM = cb2.Text YY = cb3.Text MsgBox("Month: " & MM + vbCrLf + "Day: " & DT + vbCrLf + "Year: " & YY) End Sub End Class
Now select the day, month, and year from the dropdown box and then click on the Get Date button to display the date in the form.
In this tutorial, we’ve discussed Combo Box Control in VB.net using Microsoft Visual Studio.
A ComboBox is created by dragging it from the toolbox and dropping it into the form.
It provides us with a way of presenting numerous options to the user. We can set the default item to be selected on the ComboBox when the form is loaded.
The SelectedIndexChanged event helps us specify the action to take when a particular item is selected on the Combo Box.
- Form Controls in VB.net – Windows Form Controls in VB.net
- Basic Controls in VB.net – What are the Basic Controls in VB.net
- Button Control in VB.net – Windows Form Button Control in VB.net
- Label Control in VB.net – What is Label Control in VB.net
- TextBox Control in VB.net – Properties, Method and Events of the TextBox