The List Box is to present a list of items where the user can click and select the items from the list.tems can be added at design time or at runtime. The items can also be deleted at design time or at runtime.
To add items at design time, start a new project and insert a list box on the form. Right-click on the list box to access the properties window. Next, click on collection of the Item property to bring up the String Collection Editor. Now, enter the items in the editor as shown in Figure 6.1:
After clicking on the OK button, the items will be displayed in the text box, as shown in Figure 6.2.
*Items can also be added at runtime using the Add( ) method.
Before proceeding further, we need to know that Visual Basic 2013 is an object-oriented programming language. Therefore, visual basic 2013 comprises objects. All objects have methods and properties, they are differentiated and connected by the hierarchy. For a list box, an Item is an object subordinated to the object ListBox. An Item comprises a method called Add() that is used to add items to the list box. To add an item to a list box, you can use the following syntax:
ListBox.Item.Add("Text")For example, if you wish to add a new item to ListBox1 above, you can use the following statement
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ListBox1.Items.Add("Nokia") End Sub
The item "Nokia" will be added to the end of the list, as shown in Figure 6.3
You can also let the user add their own items using the InputBox function, as follows:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim myitem myitem = InputBox("Enter your Item") ListBox1.Items.Add(myitem) End Sub
* The keyword Dim is to declare the variable myitem. You will learn more about variables in coming lessons
To remove items at design time, simply open the String Collection Editor and delete the items line by line or all at once using the Delete key.
To remove the items at runtime, use the Remove method, as illustrated in the following example. In this example, add a second button and label it "Remove Items". Click on this button and enter the following code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click ListBox1.Items.Remove("Ipad") End Sub
The item "Ipad" will be removed after running the program. You can also let the user choose which item to delete.
To clear all the items at once, use the clear method, as illustrated in the following example. In this example, add a button and label it "Clear Items"
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button2.Click ListBox1.Items.Clear() End Sub
In Visual Basic 2013, the function of the Combo Box is also to present a list of items where the user can click and select the items from the list. However, the user needs to click on the handle(small arrowhead) on the right of the combo box to see the items which are presented in a drop-down list.
To add items to the list at design time, you can also use the String Collection Editor. You will also need to type an item under the text property in order to display the default item at runtime.
Copyright©2008 Dr.Liew Voon Kiong. All rights reserved |Contact|Privacy Policy