This sample illustrates adding user-entered text to a combo box drop-down and providing lookup capability.
Adding User Text to a Combo Box Drop-Down
A combo box makes it possible for a user to enter text or select an item from a drop-down list. You can also add the text a user types in the combo box as a new item in the drop-down list so the user won't have to type the same text multiple times.
There are multiple ways to add user text to the drop-down list of a combo box, depending on the RowSourceType property setting of the combo box. In this sample, the RowSourceType of the combo boxes is 1 - Value. If the value the user enters isn't already in the drop-down list for cboCombo, the following code adds a comma and the new value to the RowSource:
cCountryName = ALLTRIM(Custs.Country) IF ATC(m.cCountryName,THIS.RowSource)=0 AND !EMPTY(m.cCountryName) THIS.RowSource=THIS.RowSource+","+m.cCountryName ENDIF
The combo box in the Manipulate Text Programmatically Sample example has a RowSourceType of 0 - None. The following code in the Valid event of cboSearchString in ...\Samples\Solution\Controls\TXT_EDT\Text.scx adds a user-entered value to the drop-down list of the combo box:
IF !EMPTY(THIS.Text) FOR i = 1 TO THIS.ListCount IF THIS.List(i) = THIS.Text RETURN ENDIF ENDFOR THIS.AddItem(THIS.Text) ENDIF
If the RowSource of the combo box is an array, you need to add the user text to the array and call the Requery method of the combo box.
If the RowSource is a table or a cursor, you need to add a record, REPLACE the blank field with the user-entered value and Requery the combo box.
Provide Lookup Capability
In addition to illustrating how to add items to a combo box, this sample demonstrates three ways of making it possible for a user to filter a table for particular values. A user can do the following:
Enter values in a combo box.
Select an item from a drop-down list.
Enter partial values in a combo box for incremental search.
Enter Values in a Combo Box
The key code is associated with the LostFocus event of cboCombo. After making sure that the user has entered a value, the LostFocus code selects the records that match the user's text and sets the RecordSource property of the grid to the result set.
cDisplayValue = ALLTRIM(THIS.DisplayValue) IF THIS.Value = "(All)" SELECT country AS location,* FROM CUSTOMER; INTO CURSOR Custs thisform.grdcust.recordsource = "Custs" ELSE SELECT country AS location,* FROM CUSTOMER ; WHERE UPPER(ALLTRIM(Customer.Country)) = UPPER(m.cDisplayValue); INTO CURSOR Custs THISFORM.grdCust.RecordSource = "Custs" ENDIF
Select an Item from a Drop-down List
Basically the same lookup code is associated with the InteractiveChange event of cboDrop вЂ“ if the Value of the combo box is "(All)", select all records into the cursor, otherwise, select the records that match the user's choice.
Enter Partial Values for Incremental Search
Code in the InteractiveChange event of cboIntSearch performs the lookup every time the user types a character in the combo box.
#DEFINE DELKEY 127 LPARAMETERS nKeyCode, nShiftAltCtrl LOCAL cDisplayValue IF nKeyCode = DELKEY cDisplayValue = ALLTRIM(THIS.DisplayValue) IF LEN(m.cDisplayValue)=1 cDisplayValue = "" ELSE cDisplayValue = LEFT(cDisplayValue,LEN(cDisplayValue)-1) ENDIF ELSE cDisplayValue = ALLTRIM(THIS.DisplayValue)+CHR(nKeyCode) ENDIF THISFORM.LockScreen = .T. DO CASE CASE EMPTY(m.cDisplayValue) THISFORM.grdCust.RecordSource = " " CASE THIS.Value = "(All)" SELECT country AS location,* FROM CUSTOMER; INTO CURSOR Custs THISFORM.grdCust.RecordSource = "Custs" OTHERWISE SELECT country AS location,* FROM CUSTOMER ; WHERE UPPER(ALLTRIM(Customer.Country)) = UPPER(m.cDisplayValue); INTO CURSOR Custs THISFORM.grdCust.RecordSource = "Custs" ENDCASE THISFORM.ResetCombos(THIS) THISFORM.LockScreen = .F.