ComboBox Control in VB.net – Properties, Methods and Events

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.

See also all the Basic Control Using VB.net with Source Code….

In order for you to test your VB.net Code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an Online Compiler in VB.net for you to test your VB.net Code for free.

We have learned how to use the TextBox Control in VB.net in the previous lesson. In this lesson we shall learn How To Write a Program for ComboBox Control in VB.net.

Let’s create a combo box by dragging a ComboBox Control from the Toolbox and dropping it on the form.

ComboBox Control in VB.net Tutorial
ComboBox Control in VB.net Tutorial

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 of the commonly used Properties of the ComboBox Control in VB.net.

#Properties of the ComboBox Control in VB.netDescription
1.AllowSelectionGets a value indicating whether the list enables selection of list items.
2.AutoCompleteCustomSourceGets or sets a custom System.Collections .Specialized.StringCollection to use when the AutoCompleteSourceproperty is set to CustomSource.
3.AutoCompleteModeGets or sets an option that controls how automatic completion works for the ComboBox.
4.AutoCompleteSourceGets or sets a value specifying the source of complete strings used for automatic completion.
5.DataBindingsGets the data bindings for the control.
6.DataManagerGets the CurrencyManager associated with this control.
7.DataSourceGets or sets the data source for this ComboBox.
8.DropDownHeightGets or sets the height in pixels of the drop-down portion of the ComboBox.
9.DropDownStyleGets or sets a value specifying the style of the combo box.
10.DropDownWidthGets or sets the width of the of the drop-down portion of a combo box.
11.DroppedDownGets or sets a value indicating whether the combo box is displaying its drop-down portion.
12.FlatStyleGets or sets the appearance of the ComboBox.
13.ItemHeightGets or sets the height of an item in the combo box.
14.ItemsGets an object representing the collection of the items contained in this ComboBox.
15.MaxDropDownItemsGets or sets the maximum number of items to be displayed in the drop-down part of the combo box.
16.MaxLengthGets or sets the maximum number of characters a user can enter in the editable area of the combo box.
17.SelectedIndexGets or sets the index specifying the currently selected item.
18.SelectedItemGets or sets currently selected item in the ComboBox.
19.SelectedTextGets or sets the text that is selected in the editable portion of a ComboBox.
20.SelectedValueGets or sets the value of the member property specified by the ValueMember property.
21.SelectionLengthGets or sets the number of characters selected in the editable portion of the combo box.
22.SelectionStartGets or sets the starting index of text selected in the combo box.
23.SortedGets or sets a value indicating whether the items in the combo box are sorted.
24.TextGets or sets the text associated with this control.
Properties and Description of the ComboBox Control in VB.net

Methods of the ComboBox Control in VB.net

The following are some of the commonly used Methods of the ComboBox Control in VB.net.

#Methods of the ComboBox Control in VB.netDescription
1.BeginUpdatePrevents the control from drawing until the EndUpdate method is called, while items are added to the combo box one at a time.
2.EndUpdateResumes drawing of a combo box, after it was turned off by the BeginUpdate method.
3.FindStringFinds the first item in the combo box that starts with the string specified as an argument.
4.FindStringExactFinds the first item in the combo box that exactly matches the specified string.
5.SelectAllSelects all the text in the editable area of the combo box.
Methods and Description of the ComboBox Control in VB.net

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.netDescription
1.FontChangedIt occurs when the property of the font value is changed.
2.FormatWhen the data is bound with a combo box control, a format event is called.
3.SelectIndexChangedIt occurs when the property value of SelectIndexChanged is changed.
4.HelpRequestedWhen the user requests for help in control, the HelpRequested event is called.
5.LeaveIt occurs when the user leaves the focus on the ComboBox Control.
6.MarginChangedIt occurs when the property of margin is changed in the ComboBox control.
Events of the ComboBox Control in VB.net

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

Program Output:

ComboBox Control in VB.net Output
ComboBox Control in VB.net Output

Now select the day, month, and year from dropdown box and then click on the Get Date button to display the date in the form.

ComboBox Control in VB.net Display Message Box
ComboBox Control in VB.net Display Message Box

Summary

In this tutorial we’ve discussed about 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.


Leave a Comment