VB Tutorial
Tools
Free Downloads
Free Link Directory

Basics
Data Types
Windows Forms
Buttons
Check Box
Text Box
Combo Box
Radio Buttons
Progress Bar
Color & Font Dialogs
Open & Save Dialogs
Process Control

More Complex Examples
Audio Player
Rotating Rectangle



ComboBox1.Items.Add Vb.Net

ComboBoxes


ComboBoxes and List Boxes have a lot in common as they both provide the user a list of text strings to choose from. You get three different types of ComboBoxes named:

Simple ComboBox:
This ComboBox displays a open list of entries from which the user can choose. The user can also add text to the ComboBox.

DropDown ComboBox:
This ComboBox displays a single entry at a time. When the user clicks on the ComboBox the list drops down and the rest of the entries are displayed, from which the user can select one.
The user can also add text to this ComboBox.

DropDownList ComboBox:
The DropDownList ComboBox can also only display one entry at a time. When the user Clicks on the ComboBox the list will drop down and all entries will be displayed. The User can only select from this ComboBox and can not enter text.

To select which type of ComboBox you want to use in your application go to the ComboBox attributes. In the attributes list you can select the DropDownStyle and choose one of the three options.

Lets create a sample application to demonstrate how ComboBoxes can be used. We will use ComboBox1.Items.Add in Vb.Net to add new contacts and their numbers.
Begin by creating a new project and call it My Contacts.vb
Set the Forms text to "My Contacts"

On the form create the following controls:
Label1, Label2
ComboBox1
TextBox1
Button1, Button2, Button3

Change Label1's text to "Name" and Label2's text to "Contact Number"
Set ComboBox1's DropDownStyle to DropDown
Remane TextBox1 to txtNumber
Rename Button1 to btnAdd and change its text to "Add Contact"
Rename Button2 to btnUpdate and change its text to "Update Number"
Rename Button3 to btnDelete and change its text to "Delete"

Our application will be able to hold 20 Names and Numbers. To test our application we will add some names and numbers in the load event of our form. We will need to set up an array to hold our data. In the code view, just below "Public Class Form1", Type "Dim MyNumber(19) As String". This will create an Array called MyNumber which will be able to hold 20 Numbers (0 to 19).
In the forms Load event, add the following text:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Add("Ann") : MyNumber(0) = "021 640 9890"
ComboBox1.Items.Add("Bruce") : MyNumber(1) = "471 223 6598"
ComboBox1.Items.Add("Charles") : MyNumber(2) = "028 350 1768"
ComboBox1.Items.Add("Diane") : MyNumber(3) = "125 617 5557"
btnUpdate.Enabled = False
End Sub

The code above added four contacts and disabled btnUpdate. Double click on the ComboBox to go to its event handling code. Change the code as follow:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal ...
txtNumber.Text = MyNumber(ComboBox1.Items.IndexOf(ComboBox1.Text))
btnUpdate.Enabled = True
End Sub

As you can remember the array MyNumber has 20 different locations, ranging from 0 to 19. The first line in the code above states that the text that should be displayed in the textbox is the data stored in MyNumber, at the same location as the ComboBox's Index. The second line enables btnUpdate so that the user can change the contacts number, should he/she wish. Arrange the control as in the figure below.


Figure 6: ComboBox

Now Run the application. You will see that when you click on the ComboBox and select a name, the textbox will show the number for that name. Lets add some code to make the buttons work so we can add or delete contacts and update existing contact's numbers. For btnAdd set the following code:

Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
ComboBox1.Items.Add(ComboBox1.Text)
MyNumber(ComboBox1.Items.IndexOf(ComboBox1.Text)) = txtNumber.Text
End Sub

For btnUpdate add the following code:

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
MyNumber(ComboBox1.Items.IndexOf(ComboBox1.Text)) = txtNumber.Text
btnUpdate.Enabled = False
End Sub

For btnDelete add:

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
On Error Resume Next
Dim temp As Integer = (ComboBox1.Items.IndexOf(ComboBox1.Text))
ComboBox1.Items.Remove(ComboBox1.Text)
For i = temp To 19
     Debug.Print(MyNumber(i))
     MyNumber(i) = MyNumber(i + 1)
Next i
txtNumber.Text = ""
btnUpdate.Enabled = False
End Sub

The application will now function properly, but you will find that every time you close the application your changes are lost. Later in this tutorial we will discuss how to write data to a file on your Hard Disk as well as retrieving it.
If you liked this article then Please Thumb This Up with a short review - Stumble It!
Continue to the next page.

 


Email us at: info@pro2visual.com
 

Get your Website Listed on our Free Link Banner Directory
Advertise your site with your own unique link banner we will also provide text links to your site.

 

Popular Articles

Moving a borderless form in vb.net

ComboBox1.Items.Add

Link Progress Bar with Timer in vb.net

How to open a folder using the process control

Start Vb.net programming. No get rich quick scam

How to Convert the Date Format

Click here for Industry Leading Development Softwa