The burden of sorting the elements to be searched often lies with the program, but with a flexible tool such as a sorted ListBox control, maintaining’ a.sorted list at all times is ea,sy.Combining a sorted list of keysmaintained by a ListBox control and the Binary Search algorithm is a powerful approach that can be used in situations ‘in which you’d normally deploy database techniques. The database objects,Database Programming with VISUalBasic) require significant overhead, which isn’t worthwhile for a small application such as Keyl.ist or BSearch.
. In Search project, the ISBN field is editable (Locked property set to False), so that the user can enter an ISBN value. As the user edits the contents of the ISBN textbox, the Change event occurs with every character typed and the following ,code is executed.
The code calls the BSea:n:hOfunction with the text entered in the txtISBN TextBoxcontrol. The BSearchO function searches the ListBox control fo r an item that partially matches its argument. If the item is found, the function returns its position in the list and highlights the key in the list. Ifthe item is not found, it returns the value -1. If the item is,found, the program also selects it in the list and calls the ListBox control’s Click event to update the contents of ‘the other TextBox controls in the data entry section of the-Form. The listing of the BSearchO function follows
The BSearch() Function
The Upper and Lower variables delimit.the section of the list that may contain the desired element. The items before Lower and after UpPer have-already been rejected. ” When the function starts, the Lower variable is “setto the position of the first item, and the Upper variable is set to the position of the last item. The position of the middle elemenfls then calculated (variable Middle), and the key field is compared with the middle element. H the key field is smaller than the middle item, the upper hall of the list is rejected, by dUjUStingthe value of the Upper variable·}he list is sorted in ascending order, that is, the “a” character shows up in the list before the “g:”) H the key field is “larger than the middle item, the lower half of the list is rejected by adjusting the value of the tower variable.
The BSearch project is identical to the KeyList project, only it uses the Binary Search algorithm to locate the first key in the ListBox control that partially or fully matches the string entered by the user in the ISBN box.
The ComboBox Control
The ComboBox control is similar to the ListBox control in the sense that it contains multiple items of which the user may select one, but it takes less space on-screen. The ComboBox is practically an expandable ListBox control, which can grow when the user wants to make a selection and retract after the selection is made. Normally, the ComboBox control displays one line with the selected item. The real difference, however, between ComboBox and ListBox controls is that the ComboBox control allows the user to specify items that don’t exist in the list. Moreover, the Text property of the ComboBox control is read-only at runtime and you can locate an item by assigning a value to the control’s Text property. Three types of ComboBox controls are available in Visual Basic 6. The value of
the control’s Style property, whose values are shown in Table 5.3, determines which box is used,
The Combos project in this chapter’s folder on the CD (see Figure 5.11) demonstrates the three styles of the.ComboBox control. It’s a common element of the Wmdows interface and its properties and methods are identical t’o those of the ListBox control. Load the Combos project in the Visual Basic IDE and experiment with the three styles of the ComboBox control.
The Droplzown and Simple Combo Box controls allow the user to select an item from the list or enter a new one in the edit box of the control. , The Drop Down List is similar to a List control in the sense that it restricts the user to selecting an item, but not entering a new one. However, it takes much less space on the Form. When the user wants to make a selection, the Drop Down List expands to display more items. Afterthe user has made a selection, the list contracts to a single line again.
Most of the properties and methods of the ListBox control also apply to the Combo Box control. The Addltem method adds items to a ComboBox, and the Remove- Item method removes items from a Combo Box. To access the items of the control, you can use the List array, and the current selection in the control is given by its ext property. You can also use the ltemData property to maintain sorted lists of keys, as you’ll see in the KeyCombo example shortly. You can even set the control’s MultiSelect property to 1 (Simple) or 2 (Extended) to allow the user to select multiple items in a ComboBox controL To find out whether an item has been selected, use the Selected property as before,
VB6 at Work: The KeyCombo Project
The KeyCombo application, shown in Figure 5.12, is similar to the KeyList application you saw earlier, but KeyCombo maintains three indexed lists, and the user can recall records based on anyone key. Each record .has four fields, three of them being potential keys. In a real application you could add any number of fields on the right-hand side of the Form.
KeyCombo is a useful Form for incorporating into a larger payment tracking .application. Customers can be recalled by their Social Security number, their AmericanExpress card number, or their VISAcard number. All three keys are unique, and only one of tl)e.fields needs to be filled out for the application to run.