VB.NET Technology

VB.Net Projects

VB.Net Project 1

ComboBox Controls
Previous Home Next
adplus-dvertising

The ComboBox control is used to save space on a dialog because the only part of the combo box that is permanently visible are the text box and button parts of the control. When the user clicks the arrow button to the right of the text box, a list box unfolds in which the user can make a selection. As soon as he or she does so, the list box disappears and the display returns to normal.

As the name implies, a combo box combines a number of controls, to be specific the TextBox, Button, and ListBox controls. Unlike the ListBox, it is never possible to select more than one item in the list of items contained in a ComboBox and it is optionally possible to type new entries in the list in the TextBox part of the ComboBox.

ComboBox Properties

NameAvailabilityDescription
DropDownStyleRead/Write

A combo box can be displayed with three different styles:

q DropDown : The user can edit the text box part of the control, and must click the arrow button to display the list part of the control.

q Simple : Same as DropDown, except that the list part of the control is always visible, much like a normal ListBox.

q DropDownList : The user cannot edit the text box part of the control, and must click the arrow button to display the list part of the control.

DroppedDownRead/Write Indicates whether the list part of the control is dropped down or not. If you set this property to true, the list will unfold.
ItemsRead/WriteThis property is a collection, which contains all the items in the list contained in the combo box.
MaxLengthRead/Write By setting this property to anything other than zero, you control the maximum number of characters it is possible to enter into the text box part of the control.
SelectedIndexRead/Write Indicates the index of the currently selected item in the list.
SelectedItemRead/WriteIndicates the item that is currently selected in the list.
SelectedTextRead/WriteRepresents the text that is selected in the text box part of the control.
SelectionStartRead/Write In the text box part of the control, this property represents the index of the first character that is selected.
SelectionLengthRead/Write The length of the text selected in the text box part of the control.
SortedRead/WriteSet this property to true to make the control sort the items in the list portion alphabetically.
TextRead/WriteIf you set this property to null, any selection in the list portion of the control is removed. If you set it to a value, which exists in the list part of the control, that value is selected. If the value doesn't exist in the list, the text is simply shown in the text portion.

ComboBox Events

NameDescription
DropDownOccurs when the list portion of the control is dropped down.
SelectedIndexChanged Occurs when the selection in the list portion of the control changed.
KeyDown, KeyPress, KeyUpThese events occur when a key is pressed while the text portion of the control has focus. Please refer to the descriptions of the events in the text box section earlier in this chapter.
TextChanged Occurs when the Text property changes

ComboBox Text Example

In following image i have designed a window form for saving the address of user. Make design of your application in following image manner and change properties according to you.

In following figure for selecting country i have used ComboBox for selecting country and after that he/she will get the name of corresponding state name of that country he/she selected.

Code for Combobox1 to add the items in Combobox2. Write following code on Combobox1 selected index change

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        comboBox2.Items.Clear()
        comboBox2.Enabled = True
        If ComboBox1.SelectedItem.ToString() = "India" Then
            comboBox2.Items.Add("NewDelhi")
            comboBox2.Items.Add("Chennai")
            comboBox2.Items.Add("Banglore")
            comboBox2.Items.Add("Mumbai")
        ElseIf ComboBox1.SelectedItem.ToString() = "Australia" Then
            comboBox2.Items.Add("Sydney")
            comboBox2.Items.Add("Johnsberg")
            comboBox2.Items.Add("Perth")
            comboBox2.Items.Add("Melborn")
        ElseIf ComboBox1.SelectedItem.ToString() = "United States" Then
            comboBox2.Items.Add("NewYork")
            comboBox2.Items.Add("Los Ageles")
            comboBox2.Items.Add("California")
            comboBox2.Items.Add("LosBegas")
        End If
    End Sub

After that choosing State name he/she will get the location in ComboBox 3. Like in following figure.

Code for ComboBox2 is given below write that code on Combobox2 Selected Index Changed

Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
        comboBox3.Items.Clear()
        comboBox3.Enabled = True
        If ComboBox2.SelectedItem.ToString() = "NewDelhi" Then
            comboBox3.Items.Add("East Delhi")
            comboBox3.Items.Add("West Delhi")
            comboBox3.Items.Add("North Delhi")
            comboBox3.Items.Add("South Delhi")
        ElseIf ComboBox2.SelectedItem.ToString() = "Chennai" Then
            comboBox3.Items.Add("Chennai")
        ElseIf ComboBox2.SelectedItem.ToString() = "Mumbai" Then
            comboBox3.Items.Add("Mumbai")
            comboBox3.Items.Add("Navi Mumbai")
        ElseIf ComboBox2.SelectedItem.ToString() = "Banglore" Then
            comboBox3.Items.Add("Banglore")
        ElseIf ComboBox2.SelectedItem.ToString() = "Johnsberg" Then
            comboBox3.Items.Add("Johnsberg")
        ElseIf ComboBox2.SelectedItem.ToString() = "Sydney" Then
            comboBox3.Items.Add("Sydney")
        ElseIf ComboBox2.SelectedItem.ToString() = "Perth" Then
            comboBox3.Items.Add("Perth")
        End If
    End Sub

Code for Save Button Click

Private Sub Button1_Click_1(ByVal sender As System.Object, 
	ByVal e As System.EventArgs) Handles Button1.Click
        Dim output As String
        output = "Name: " & Convert.ToString(Me.textBox1.Text) & vbCr & vbLf
        output += "Address : " & Convert.ToString(Me.textBox2.Text) & vbCr & vbLf
        output += "          " & Convert.ToString(Me.textBox3.Text) & vbCr & vbLf
        output += "          " & Convert.ToString(Me.comboBox3.Text) & vbCr & vbLf
        output += "          " & Convert.ToString(Me.ComboBox2.Text) & vbCr & vbLf
        output += "Country:  " & Convert.ToString(Me.ComboBox1.Text) & vbCr & vbLf
        textBox4.Text = output
    End Sub

Final you will get output Like this.

Previous Home Next