Http://www.week5.net December 01, 2004
In the Delphi language, a lot of data entered is made, such as selecting or manually entered controls from the database: DBListBox, DBcombox, DblookUpComboBox, etc. But for such an example: the database name is DM.DB, two fields: Code: Code Name: Name Requires the name corresponding to the code according to the code input by the user. The general user does not know the correspondence of code and name, such as allowing the user to enter code, select the corresponding name, because the above controls do not allow the operator to see the correspondence of the code and name, such as letting the user pull the box according to the code To find the name of the corresponding record, it will be difficult to operate. According to this situation, I have prepared the following program that puts DBGrid as the EDIT drop-down list box assist operation, and the corresponding relationship of the code and name is directly displayed in the DBGRID, and can update DBGRID at any time according to the change of the code in the DBGRID. The record pointer, the user can intuitively and conveniently take the name you need, and the DBGRID will appear in accordance with the user in the EDIT, and the EDIT box disappears will disappear. This approach provides convenience to users, and does not affect the overall beauty of the interface, and the effect is good. Now it provides everyone, you can process processes to procedural according to your own needs, applied to program development, and hope to take a brick-to-jade role. [Problem]: Doing such a small program: let the user enter the code, then display the name on the form. 1. First we can build a form, add controls in this form: Table: Table1, set its property to correspond to the code library DM.DB, and set the Active to True DataSource: DataSource1, set its properties DataSet to table1 Edit: CodeEdit, NameEdit corresponds to code input box and name display box DBGRID: DBGRID1, sets its property Datasource to DataSource1 and sets the value of the value of the CodeEdit attribute text, and the value of NameEdit TEXT is empty.
2, against the following statement, modify the CodeEdit OnEnter, OnExit, OnKeyDown, OnKeyUp event: in the event OnEnter CodeEdit follows: procedure TForm1.CodeEditEnter (Sender: TObject); begin if CodeEdit.text <> "" then begin CodeEdit.SelStart: = length (CodeEdit.text); Table1.locate ( "code", CodeEdit.text, [lopartialkey]); End; end; CodeEdit OnExit event of the following: procedure TForm1.CodeEditExit (Sender: TObject); begin if activecontrol <> DBGRID1 THEN BEGIN DBGRID1.Visible: = false; table1.locate ("code", codeEdit.text, [loPartialKey]); if Table1.eof the beginning; exit; end; if not table1.eof dam begin codeedit.Text: = Table1.fieldbyname ( "code") asstring; NameEdit.Text:. = Table1.fieldbyname ( "name") asstring; end; end; end; CodeEdit the OnKeyDown event follows:. Procedure Tform1.CodeEditKeyDown ( Sender: Tobject; Var Key: Word; Shift: TshiftState; var i: integer; begin if (table1.recordcount>) The begin case key of 48..57: begin dbgrid1.visible: = True; Table1.Locate ("CODE", CodeEdit.text, [loPartialKey]); END; vk_next: if DBGrid1.visible the begin i: =; while (not table1.eof) and (i <11) Do Begin Table1 .Next; i: = i 1; end; code; (i <11) do begin table1.prior; i: = i 1; end; codeEdit.text: = Table1.fieldbyname ("code"). asstring;