|
|
|||
|
Basics More Complex Examples
|
ComboBox1.Items.Add Vb.NetComboBoxes
Simple ComboBox:
DropDown ComboBox:
DropDownList ComboBox:
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.
Change Label1's text to "Name" and
Label2's text to "Contact Number"
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).
Private Sub Form1_Load(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles MyBase.Load 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 ... 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.
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 For btnUpdate add the following code:
Private Sub btnUpdate_Click(ByVal
sender As System.Object,
ByVal e As
System.EventArgs) Handles btnUpdate.Click For btnDelete add:
Private Sub btnDelete_Click(ByVal
sender As System.Object, ByVal e
As System.EventArgs)
Handles btnDelete.Click
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.
Email us at: info@pro2visual.com
|
Popular Articles Moving a borderless form in vb.net Link Progress Bar with Timer in vb.net How to open a folder using the process control |
|
|
|