VB Default Controls List Box Combo Box Slide
VB Default Controls List Box, Combo Box Slide 1
Default Controls Slide 2
List Box List. Box control can used the to present a list of choices. v The key parts of the list box are the following: v – Item list: This is the list of items from which the users can select. These items are added to the list in the design environment or by the program as it is running. – Selected item: This item is chosen by the users. – Scrollbar: This part indicates that more items are available on the list than will fit in the box. Slide 3
Adding Items to List Box at design-time List box items can be added at design time by using the “List” property of the control. v After you add an item to the list, press Ctrl+Enter to move to the next line of the list. v Slide 4
Adding Items to List Box at Programmatically v Using add. Item method: – Example: Private Sub Form_Load() Dim i As Integer For i = 1 To 100 List 1. Add. Item “This is item “ & i Next i End Sub Slide 5
Other List Box Methods & properties v To clear all the items in a list box, use the Clear method: – List 1. Clear v To refer to a list item use the list() array: – List 1. list(0) – List 1. list(1) v First Item Second item List. Count represents the number of items in a list box. For i = 0 To List 1. List. Count - 1 Print List 1. List(i) Next i Slide 6
Other List Box Methods & properties v List. Index property represents the selected item. – Listindex is – 1 if no item is selected. – Msgbox List 1. List(List 1. List. Index) v Text property contains the selected item text, If no list box item has yet been clicked, the Text property contains an empty string (“”). – Msg. Box List 1. Text v To remove items from the list box, use the Remove. Item method: – List 1. Remove. Item List 1. List. Index Slide 7
List Box Items Position v To add an item in a specific position (index): – lst. Available. Add. Item “Apples”, 2 v To sort list box item, set the Sorted property to True. (designtime only) Slide 8
Appearance of a List Box v Style property: – Standard – Checkbox v Columns property: – 0 for single columns – 1, 2, 3, … for the number of columns appearing at one time Slide 9
Working with Multiple Selections v Multi. Select property allows the user to select more than one item: – 0 : None u only one selection at a time – 1 : Simple u users can click an item with the mouse to select it or click a selected item to deselect it – 2 : Extended u users can select a range of items by clicking the first item in the range and then, while holding down the Shift key, clicking the last item in the range; all items in between the first and last item are selected. To add or delete a single item to or from this selection, users hold down the Ctrl key while clicking the item. Slide 10
Working with Multiple Selections List. Index property works only for a single selection v You have to examine each item in the list to determine whether it is selected. v Whether an item is selected is indicated by the list box’s Selected property. v – True (item selected) – False (Item not selected) – Example: For I = 0 to List 1. List. Count - 1 If List 1. Selected(I) Then Print List 1. List(I) Next I Slide 11
Keeping Other Data in the List v v Item. Data property of a list box is an array of long integers, one for each item that has been added to the list box. New. Index property contains the index number of the most recently added item in the list. lst. CCode. Add. Item “Egypt” lst. CCode. Item. Data(lst. CCode. New. Index) = 20 lst. CCode. Add. Item “Saudi Arabia” lst. CCode. Item. Data(lst. CCode. New. Index) = 966 v Retreving the Data of an Item: – Msgbox lst. CCode. Item. Data(lst. CCode. List. Index) Slide 12
The Combo Box Enables you to present lists to the users v can be used in three different forms: v – The drop-down combo box: Presents the users with a text box combined with a dropdown list. The users can either select an item from the list portion or type an item in the text box portion. – The simple combo box: Displays a text box and a list that doesn’t drop down. the users can either select an item from the list portion or type an item in the text box portion. – The drop-down list: Displays a drop-down list box from which the users can make a choice. The users cannot enter items that are not in the list. Slide 13
The drop-down combo box Slide 14
The simple combo box Slide 15
The drop-down list Slide 16
Combo Box features Use the Add. Item, Remove. Item, and Clear methods to modify the contents of the list. v Sorted or an unsorted list. v Support the Item. Data array and New. Index property. v No multiple choices v Combo boxes Allows the users to enter choices that are not on the list. v Drop-down list is useful for presenting several choices in a small amount of space. v Slide 17
Creating a Combo Box Draw a combo box on your form v Select its style from the property window v Add items to the List property at design or run time v Slide 18
- Slides: 18