Anda di halaman 1dari 5

ListBox Event The default event of ListBox is the SelectedIndexChanged which looks like this in code: Private Sub

ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged End Sub Working with ListBoxes Drag a TextBox and a ListBox control to the form and add some items to the ListBox with it's items property. Referring to Items in the ListBox Items in a ListBox are referred by index. When items are added to the ListBox they are assigned an index. The first item in the ListBox always has an index of 0 the next 1 and so on. Code to display the index of an item Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged TextBox1.Text = ListBox1.SelectedIndex 'using the selected index property of the list box to select the index End Sub When you run the code and select an item from the ListBox, it's index is displayed in the textbox. Counting the number of Items in a ListBox Add a Button to the form and place the following code in it's click event. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _ As System.EventArgs) Handles Button1.Click TextBox1.Text = ListBox1.Items.Count 'counting the number of items in the ListBox with the Items.Count End Sub

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

Working with ComboBoxes Drag a ComboBox and a TextBox control onto the form. To display the selection made in the ComboBox in the Textbox the code looks like this: Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = ComboBox1.SelectedItem 'selecting the item from the ComboBox with selected item property End Sub Removing items from a ComboBox You can remove all items or one particular item from the list box part of the ComboxBox. Code to remove a particular item by it's Index number looks like this: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles Button1.Click ComboBox1.Items.RemoveAt(4) 'removing an item by specifying it's index End Sub Code to remove all items from the ComboBox Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_ System.EventArgs) Handles Button1.Click ComboBox1.Items.Clear() 'using the clear method to clear the list box End Sub

Anda mungkin juga menyukai