ARTICLE

ComboBox Items in VB.NET

Posted by Mahesh Chand Articles | Windows Controls June 08, 2010
This article explains how to add, read, find and sort items in a ComboBox.
Download Files:
 
Reader Level:

Read ComboBox in VB.NET Part 1 here

ComboBox Items


The Items property is used to add and work with items in a ComboBox. We can add items to a ComboBox at design-time from Properties Window by clicking on Items Collection as you can see in Figure 5.

ComboBoxImg5.jpg
Figure 5

When you click on the Collections, the String Collection Editor window will pop up where you can type strings. Each line added to this collection will become a ComboBox item. I add four items as you can see from Figure 6.

 

ComboBoxImg6.jpg
Figure 6

The ComboBox looks like Figure 7.

ComboBoxImg7.jpg
Figure 7

You can add same items at run-time by using the following code snippet.

ComboBox1.Items.Add("Mahesh Chand")

ComboBox1.Items.Add("Mike Gold")

ComboBox1.Items.Add("Praveen Kumar")

ComboBox1.Items.Add("Raj Beniwal")

Getting All Items

To get all items, we use the Items property and loop through it to read all the items.  The following code snippet loops through all items and adds item contents to a StringBuilder and displays in a MessageBox.

Dim sb As New System.Text.StringBuilder

        For Each item In ComboBox1.Items

            sb.Append(item)

            sb.Append(" ")

        Next

MessageBox.Show(sb.ToString())

Selected Text and Item

Text property is used to set and get text of a ComboBox. The following code snippet sets and gets current text of a ComboBox.

ComboBox1.Text = "Mahesh Chand"

MessageBox.Show(ComboBox1.Text)

 

We can also get text associated with currently selected item by using Items property.

Dim selectedItem As String = ComboBox1.Items(ComboBox1.SelectedIndex)

Why the value of ComboBox.SelectedText is Empty?
SelectedText property gets and sets the selected text in a ComboBox only when a ComboBox has focus on it. If the focus moves away from a ComboBox, the value of SelectedText will be an empty string. To get current text in a ComboBox when it does not have focus, use Text property.

DataSource
DataSource property is used to get and set a data source to a ComboBox. The data source can be a collection or object that implements IList interface such as an array, a collection, or a DataSet. The following code snippet binds an enumeration converted to an array to a ComboBox.

ComboBox1.DataSource = System.Enum.GetValues(GetType(ComboBoxStyle))

DropDownStyle
DropDownStyle property is used to gets and sets the style of a ComboBox. It is a type of ComboBoxStyle enumeration.
The ComboBoxStyle enumeration has following three values.

·         Simple - List is always visible and the text portion is editable.

·         DropDown – List is displayed by clicking the down arrow and that the text portion is editable.

·         DropDownList - List is displayed by clicking the down arrow and that the text portion is not editable.
The following code snippet sets the DropDownStyle property of a ComboBox to DropDownList.

ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList

DroppedDown

If set true, the dropped down portion of the ComboBox is displayed. By default, this value is false.

Sorting Items

The Sorted property set to true, the ComboBox items are sorted. The following code snippet sorts the ComboBox items.

ComboBox1.Sorted = True

Find Items

The FindString method is used to find a string or substring in a ComboBox. The following code snippet finds a string in a ComboBox and selects it if found.

Private Sub FindStringButton_Click(ByVal sender As System.Object, _

    ByVal e As System.EventArgs) Handles FindStringButton.Click

        Dim index As Integer = ComboBox1.FindString(TextBox1.Text)

        If (index < 0) Then

            MessageBox.Show("Item not found.")

            TextBox1.Text = String.Empty

        Else

            ComboBox1.SelectedIndex = index

        End If

End Sub


Continue reading ComboBox in VB.NET Part 3 here

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Become a Sponsor