The Remove Selected Items Button VB Help

The code examines the control’s SeICount property, which specifies the number of selected items. If SelCount equals one, it moves the item. If the number of selected items is more than one, the program scans the entire list and removes the items that have their Selected property set to True. Notice also that the list is scanned backward, as explained in the discussion of the RemoveItem method. The Arrow Buttons The two Command buttons between the two ListBox controls shown in Figure 5.7 transfer selected items from one list to another. The first arrow button can transfer a single element only, after it ensures that the list contains a selected item. First, it adds the item to the second list, and then it removes the item from the original list.


The Right Arrow Button


The second arrow button transfers items in the opposite direction. Its code is similar to that of the Remove Selected Items button. The arrow button examines the SeICount property, and if a single item is selected, it moves the item to the other list with the commands of the previous list. If multiple items are selected, the arrow button scans the list backward, ..copying and deleting each. selected item, Its code is as follows.

Before we leave the topic of the UstBox control, let’s examine one more powerful technique: using the UstBox control to maintain a list of keys (the data items used in recalling the information) to an array or random access file with records of related information.

Indexing with the  ListBox Control

A key property of the ListBox control is the ItemData property, which is an array similar to the Lis~array, but instead of containing strings that appear in the control, it contains nUmbers. Each item displayed on a Listbox control has two entries: a string, given by the list’s Ust(i) property; and a number, given by the ltemData(i) property (i is the index of the item in the list).’ The ltemData property is a Long value that can store any type of numeric information associated with each item, as long as this information doesn’t need .tobe displayed on the list. If you maintain a list of employee names, tne ListO array Cell hold names, and the ItemDataO array can hold the salary of each employee. Each employee can be accessed by name, but his or her salary can appear in a TextBox control on the same Form.
The real value of the ItemData property, however, is not for storing additional.  pieces of information along with the item. If you want to store more information than is visible on the list, you probably would want to store more information than just a number, Suppose you need to maintain a list of names and addresses. Storing each entry-in a ListBox control isn’t practical. The control would have to be very
wide to accommodate the entire string with each person’s name, address, city,phone numbers, and so on. A more practical approach is to store the names of the persons in the ListBox control and use the names as keys to access the elements of an array in which the rest of the information is stored. Each record is stored in an array element, which should match one of the items in the ListBox control. If the item’s ItemData propertyis set to the index of the corresponding array element, you can access the array records instantly. The same approach would also work with random access files, only this time, the value of the ItemData property would be the number of the matching record. The KeyList application demonstrates this technique.

VB6 at Work: The KeyList Project

KeyList, shown mFigure 5.8, maintains a list of books, indexed by ISBN. It could be a list of names and addresses, a price list, or any other collection of-related items. The application is simple and doesn’t justify the design of a database. To save space and time, the information is stored in an array and saved to a disk file between sessions.

The array is a convenient storage mechanism for a few hundred entries, but it can’t be sorted easily. Inserting each item in the proper array element requires massive copying. To insert a new item in the array’s first element, you’d have to copy all the items to the next array position. In general, you should avoid sorting’ whenever possible, especially if it has to be repeated at runtime.

The ListBox control is frequently used as an-array, especially to maintain sorted items. You can even hide the control by setting its Visible property to Falseand still use it in your application as an array. It provides the same functionality and can maintain its sorted items at all times.

The KeyList application uses a ListBox control to maintain a sorted list of items. We could have stored all the information in the ListBox control.but this is hardly practical. The list will be used for storing the keys which are the books’ ISBNnumbers. The remaining fields can be stored in an array. Figure 5.9 shows how the keys are stored in the ListBox control and the matching data in the array. The ltemData array points to the appropriate element in the Data Array, which may contain a large number of fields. All you need is a way to connect the ISBN numbers to the corresponding information in the array. This link is provided by the ListBox control, with its ItemData .4Iproperty.The ItemData property is an array of numbers, one per list item. Each item in the list has a value (what is displayed on the list) that can be accessed via the List property and a related value that isn’t displayed on the List, but that can be accessed with the ItemData property. The first element in the list is L;stl. List(O), and the related information is stored in the fourth array element, whose index is given bythe property L;stl. Itenilata(O). The List’s Sorted property is set to True so that the user sees the keys sorted and can easily locate any item in the List The fields that correspond to each key, though, are appended to the array. As a programmer, you don’t have to worry about maintaining the elements of the array in any order. As long as you can instantly access the array ,element that corresponds to the selected key in the List control, it’s as if the array is sorted too.

Posted on November 5, 2015 in Basic ActiveX Controls

Share the Story

Back to Top