4.3 ListBox
ListBox is a control that displays a list of items. It allows the user to click and select the items from the list. Items can be added by the programmer at design time or at runtime using a code. We can also write code to allow the user to add items to the ListBox or remove the items from it.
4.3.1 Adding Items to ListBox
a) Adding items using the String Collection Editor
To demonstrate how to add items at design time, start a new project and insert the ListBox control on the form. Next, right-click on the list box to access the properties window. In the properties window, click on collection of the Item property to open the String Collection Editor . In the Editor, enter one item at a time, pressing the Enter key after each item, as shown in Figure 4.4. After clicking the OK button, the items will be displayed in the list box, as shown in Figure 4.5
Figure 4.4
Figure 4.5
b) Adding Items using the Add () Method
Items can also be added at runtime using the Add() method. Before we proceed further, we should know that Visual Basic 2022 is an object-oriented programming language. Therefore, visual basic 2022 comprises objects. All objects have methods and properties. They can be differentiated and connected by a hierarchy. For ListBox, an Item is an object subordinated to the object ListBox . The Item object comprises a method call Add() that is used to add items to the list box. To add an item to the list box, use the following syntax:
ListBox.Item.Add("Text") |
Example 4.3 Adding an Item to ListBox
In this example, running the program will add the item “Vivo” to the end of the list, as shown in Figure 4.6
Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click MyListBox.Items.Add("Vivo") End Sub |
Figure 4.6
Example 4.4 Adding Items using InputBox
This program allows the user to add items via an input box. First, we create a variable myitem and then assign a value to it via the InputBox function. We use the Add () method to add the user’s item into the list box. The code is as follows:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
|
The runtime interface is as shown in Figure 4.7
Figure 4.7
After typing the item ‘Vivo” in the input box, the item will be added to the list box, as shown in Figure 4.8.
Figure 4.8
Example 4.5 Geometric Progression
This is a program that generates a geometric progression and displays the results in a list box. Geometric progression is a sequence of numbers where each subsequent number is found by multiplying the previous number with a fixed number. This fixed number is called the common ratio. The common ratio can be a negative number, an integer, a fraction, and any number but it must not be a zero or 1.
The formula to find the nth term of the geometric progression is arn-1 , where a is the first number and r is the common ratio.
In this program, we employ the Do...Loop Until statement to generate the numbers in a geometric progression. To design the UI, we insert three text boxes for the user to enter the first number, the common ratio and the number of terms. We also insert a list box to display the generated numbers. Besides that, a command button is needed for the user to click and generate the geometric progression. In addition, we also add a button for clearing the list of generated numbers.
To add the numbers to the list box, we use the Add() method. The syntax is ListBox1.Items.Add(x), where x can be any variable.
The code
Private Sub BtnComp_Click(sender As Object, e As EventArgs) Handles BtnComp.Click End Sub |
The output is as shown in Figure 4.9
Figure 4.9 The runtime interface
4.3.2 Removing Items from a List Box
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, you can use the Remove method, as illustrated in Example 4.6.
Example 4.6 Removing Items from ListBox
In this example, add a 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 MyListBox.Items.Remove("iPhone") End Sub |
The item “iPhone” will be removed after running the program, as shown in Figure 4.10
Figure 4.10
Example 4.6 Removing an Item from ListBox via an Input Box
You can allow the user to choose an item to delete via an input box. To add this capability, insert a button at design time and change its text to Delete Item. Click on the button and enter the following statements in the code window:
Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click Dim myitem End Sub
|
The runtime interface is as shown in Figure 4.11. After entering the item iPhone in the input box and press OK, the item iPhone will be deleted from the list box.
Figure 4.11
To remove a selected item from the list box, using the following syntax:
Listbox1.Items.Remove(ListBox1.SelectedItem) |
Example 4.7 Delete a Selected Item from ListBox
Private Sub BtnDelSel_Click(sender As Object, e As EventArgs) Handles BtnDelSel.Click MyListBox.Items.Remove(MyListBox.SelectedItem) End Sub
|
When the user run the program and select an item to delete, the item will be deleted, as shown in Figure 4.12 and Figure 4.13.
Figure 4.13
To remove multiple selected items from the list box, you must use the If...End If statements together with the For...Next loop. Besides that, you must ensure that the list box allows multiple selection. To enable multiple selection, set the selection mode to MultipleSimple in the ListBox properties window. The code is as shown in Example 4.8.
Example 4.8 Delete Multiple Selected Items from ListBox
In this example, add an extra button to the previous example and label it as Clear Selected Items. Key in the following code:
Private Sub BtnDelSelected_Click(sender As Object, e As EventArgs) Handles BtnDelSelected.Click If MyListBox.SelectedItems.Count > 0 Then End Sub |
To clear all the items at once, use the clear method, as illustrated in Example 4.8.
Example 4.9 Clear All Items from ListBox
In this example, add a button and label it “Clear the List”
Private Sub BtnClr_Click(sender As Object, e As EventArgs) Handles BtnClr.Click End Sub |
When you run the program and click the “Clear the List” button, all the items will be cleared. The design interface for removing the items from the list box is as shown in Figure 4.14
Figure 4.14
0 Comments
Thanks for your comment.
i will reply you soon.