VB.NET identifies each item in a list box using a number called the SelectedIndex. The first item in the listbox has a SelectedIndex value of 0, the second item has a ListIndex value of 1, etc. Items may be added, removed or selected in a. Listbox using the appropriate methods.
VB.NET identifies each item in a list box using a number called the SelectedIndex. The first item in the listbox has a SelectedIndex value of 0, the second item has a ListIndex value of 1, etc. Items may be added, removed or selected in a. Listbox using the appropriate methods.
VB.NET identifies each item in a list box using a number called the SelectedIndex. The first item in the listbox has a SelectedIndex value of 0, the second item has a ListIndex value of 1, etc. Items may be added, removed or selected in a. Listbox using the appropriate methods.
The ListBox A ListBox control is used to display a list of items from which a user can select one or more. If the number of items exceeds the number that can be displayed, a scroll bar is automatically added to the ListBox control. VB .NET identifies each item in a list box using a number called the SelectedIndex - the first item in the listbox has a SelectedIndex value of 0, the second item in the listbox has a ListIndex value of 1, etc. SelectedIndex is 0 SelectedIndex is 1 SelectedIndex is 2 . . . SelectedIndex is 7 Items may be added, removed or selected in a listbox using the appropriate methods. The most common properties and methods of the listbox are given below. Properties Items refers to the entire contents of the listbox. Any single item may be accessed by its SelectedIndex value using the notation Items.SelectedIndex. The first item is numbered 0, the next is 1, and so on. For example Items.SelectedIndex = 0 refers to the first item, Items.SelectedIndex = 1 refers to the second item, and so on Items.Count is a number that refers to the number of items currently in the listbox. SelectedIndex is a number that refers to an item in the listbox. The numbering starts at 0, that is the first item in the listbox has a SelectedIndex value of 0, the second item in the listbox has a ListIndex value of 1, e.t.c. Sorted Can be either: True the items are sorted into alphabetic order Or False the items appear in the order that they are entered Text contains the currently highlighted item in the listbox . Methods Items.Add( ) adds an item to the end of the list, e.g. lstCountries.Items.Add(Scotland) Items.Clear( ) removes all the items from the listbox, e.g. lstCountries.Items.Clear() Items.RemoveAt( ) removes an item identified by its SelectedIndex value from the listbox, e.g. lstCountries.Items.RemoveAt(lstCountries.SelectedIndex) LCS, Edinburgh Page 1 of 3 Visual Basic .NET Using Controls The first item in the list has SelectedIndex 0, and the value of the Items.Count property is always one more than the largest SelectedIndex value. If no item is selected, the SelectedIndex property has value -1. To add or delete items in a ListBox control, use the Items.Add or Items.RemoveAt methods. Alternatively, you can add items to the list by using the List property at design time. Example: This example allows a user to add, delete and clear names of countries from a listbox. Any name that the user selects in the listbox is also displayed in a large font in a label. The listbox initially contains 4 countries when the program is run. The example is trivial, but it does demonstrate a number of the methods and properties of a listbox. The interface is as follows: lblSelected grpAddItem txtEnter btnAdd btnDelete btnClear lstCountries lblTotal The enabled property for the Add button is initially set to false, and only set to true whenever the user types text into the textbox. There are event procedures for the click event of the Add, Delete and Clear buttons. There is also an event procedure for clicking on an item in the listbox. The form load event is used to copy the initial number of items in the listbox into lblTotal. The keypress event for the textbox has been used to enable the Add button when text is entered into the textbox.
LCS, Edinburgh Page 2 of 3 Visual Basic .NET Using Controls The code is:
Pr i vat e Sub bt nAdd_Cl i ck( . . . ) Handl es bt nAdd. Cl i ck l st Count r i es. I t ems. Add( t xt Ent er . Text ) ' Add t ext i n t ext box i nt o l i st box t xt Ent er . Text = " " ' copy an empt y st r i ng i nt o t ext box t xt Ent er . Focus( ) ' set t he f ocus i n t ext box bt nAdd. Enabl ed = Fal se ' di sabl e t he Add but t on l bl Tot al . Text = l st Count r i es. I t ems. Count ' copy number of i t ems i n l i st box i nt o l bl Tot al End Sub
Pr i vat e Sub t xt Ent er _KeyPr ess( . . . ) Handl es t xt Ent er . KeyPr ess bt nAdd. Enabl ed = Tr ue ' enabl e Add but t on when t ext i s ent er ed i nt o t ext box
End Sub
Pr i vat e Sub bt nDel et e_Cl i ck( . . . ) Handl es bt nDel et e. Cl i ck l st Count r i es. I t ems. RemoveAt ( l st Count r i es. Sel ect edI ndex) ' del et e sel ect ed i t emf r oml i st box l bl Tot al . Text = l st Count r i es. I t ems. Count ' copy number of i t ems i n l i sbox i nt o l bl Tot al End Sub
Pr i vat e Sub bt nCl ear _Cl i ck( . . . ) Handl es bt nCl ear . Cl i ck l st Count r i es. I t ems. Cl ear ( ) ' cl ear al l i t ems f r oml i st box l bl Tot al . Text = l st Count r i es. I t ems. Count ' copy number of i t ems i n l i st box i nt o l bl Tot al End Sub
Pr i vat e Sub l st Count r i es_Cl i ck( . . . ) Handl es l st Count r i es. Cl i ck l bl sel ect ed. Text = l st Count r i es. I t ems( l st Count r i es. Sel ect edI ndex) ' copy sel ect ed i t emf r oml i st box i nt o l bl Sel ect ed End Sub
Pr i vat e Sub For m1_Load( . . . ) Handl es MyBase. Load l bl Tot al . Text = l st Count r i es. I t ems. Count ' copy number of i t ems i n l i st box i nt o l bl Tot al End Sub