Page 64 - Open Soource Technologies 304.indd
P. 64
Event Driven Programming
When you run the code and click the Button it will display the number of items available in the
ListBox.
Code to display the item selected from ListBox in a TextBox
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,_
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
TextBox1.Text = ListBox1.SelectedItem
'using the selected item property
End Sub
When you run the code and click an item in the ListBox that item will be displayed in the TextBox.
Code to Remove items from a ListBox You can remove all items or one particular item from
the list box.
Code to remove a particular item
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
As System.EventArgs) Handles Button1.Click
ListBox1.Items.RemoveAt(4)
'removing an item by specifying it's index
End Sub
Code to Remove all items
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
'using the clear method to clear the list box
End Sub
3.4.7 ComboBox
ComboBox is a combination of a TextBox and a ListBox. The ComboBox displays an editing field
(TextBox) combined with a ListBox allowing us to select from the list or to enter new text.
ComboBox displays data in a drop-down style format. The ComboBox class is derived from the
ListBox class. Figure 3.17 is the image of a ComboBox.
58 LOVELY PROFESSIONAL UNIVERSITY