The edit box is also a commonly used component in Windows programs, which is used primarily for entering single-line text. The following explains some of the common properties of the edit box.
Some of the commonly used events are: OnChange, OnKeyPress, OnEnter events, which are briefly described below:
The three edit box events described above are very useful and are often used in Delphi programming. With these three events, you can implement some of the most useful features:
| Property |
Description |
| PasswordChar |
This property is available in some edit box components such as Tmaskedit, Tdbedit. By default, this property is #0, that is, there is no mask. The user can set a mask on its own, such as ' * ', the characters that the user enters in the edit box will be displayed with the ' * ' character and hide the real characters. |
| AutoSize |
Determines whether the edit box changes size automatically with font changes, and defaults to True |
| Text |
This property is used to display and save the string in the edit box |
| MaxLength |
Determines the maximum number of characters that can be entered in the edit box, with a default of 0, indicating that any number of characters can be entered |
| ReadOnly |
Determines whether the user can change the contents of the edit box when the program is running. True to indicate that editing is not possible. |
| AutoSelect |
Determines whether the text in the edit box is automatically selected when the edit box gets the input focus |
| Event |
Description |
| OnChange |
When the content in the edit box changes, it triggers the event, which is one of the most common and useful events in edit |
| OnKeyPress |
When a key is pressed, the event is triggered |
| OnEnter |
When the edit box gets the input focus, the event is triggered |
1. Using the OnEnter event, you can give hints or trigger other events when the edit box obtains input focus. The following example gives a hint when the edit box Edit1 gets the input focus and invalidates the Button1.
Procedure Tform1.edit1enter (Sender:tobject);
Begin
ShowMessage (' Please enter your password ');
Button1.enabled:=false;
End
2. Using the onkeypress event, you can limit the type of characters entered in the edit box. The following example restricts the editing box Edit1 to entering only numbers, not other characters. If you press the non-numeric key, a beep will be made.
Procedure tform1.edit1keypress (Sender:tobject; var key:char);
Begin
If not (key in[' 0 ' ... ') 9 ', #8]) then
Begin
Key:= #0;
MessageBeep (-1);
End
End
In the upper routine, the key is a pressed character, and the Not method is used to determine whether the character entered is a number (0 to 9) or a Delete key #8 (note that you do not even block the DELETE key). If non-numeric input, use the statement "key:= #0" to mask it, #0为空, to indicate that there is no input.
3. Using the onchange event, you can trigger other events, such as making the button active or invalid, to move the input focus to a control when something changes in the contents of the edit box. The following routine causes the Button1 to take effect when the content in the edit box Edit1 is ' 123456 ' and shifts the input focus to the Button1.
Procedure Tform1.edit1change (Sender:tobject);
Begin
If edit1.text= ' 123456 ' then
Begin
Button1. Enabled:=true;
Form1.focuscontrol (button1);
End
End
Below we'll write a simple authentication program using these features of the edit control, with two labels, two edit, and two BITBTN controls in the program. The MaxLength attribute values of Edit1 and Edit2 are 6,edit2 PasswordChar as ' * ', in addition the Edit1 restricts input and receives only numbers.
When the user enters 6 digits in the Edit1, the input focus automatically jumps to the Edit2, and the OK button is activated and the input focus is obtained when confirming that the number and password in Edit1 and Edit2 are correctly entered (the number is 950755 and the password is 123456). The program interface is shown in the following illustration:
Unit Unit1; Interface uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Buttons; Type TFORM1 = Class (Tform) Label1:tlabel; Label2:tlabel; Edit1:tedit; Edit2:tedit; BITBTN1:TBITBTN; BITBTN2:TBITBTN; Procedure Bitbtn2click (Sender:tobject); Procedure Formcreate (Sender:tobject); Procedure Edit1change (Sender:tobject); Procedure Edit2change (Sender:tobject); Procedure Bitbtn1click (Sender:tobject); Procedure edit1keypress (Sender:tobject; var key:char); Private {Private declarations} public {public declarations} end; var Form1:tform1; Implementation {$R *. DFM} procedure Tform1.bitbtn2click (Sender:tobject); Begin close; Closes the form end; Procedure Tform1.formcreate (Sender:tobject); Begin edit1.text:= '; When creating a form, let the edit box be empty edit2.text:= '; Bitbtn1.enabled:=false; End Procedure Tform1.edit1change (Sender:tobject); The enter focus jumps to Edit2 if length (edit1) when the Edit1 in the edit box is 6 characters. Text) =6 then Form1. Focuscontrol (EDIT2); End Procedure Tform1.edit2change (SeNder:tobject); Begin//If the input in the two edit boxes is correct, the OK button is activated and gets the input focus if (edit2.text= ' 123456 ') and (edit1.text= ' 950755 ') THEN begin BITBTN1. Enabled:=true; Form1. Focuscontrol (BITBTN1); End End Procedure Tform1.bitbtn1click (Sender:tobject); Begin ShowMessage (' You have passed authentication. '); Close End Procedure tform1.edit1keypress (Sender:tobject; var key:char); Enter the Begin//Limit edit box to receive only the number if not (key in[' 0 ' ... '). 9 ', #8]) then begin key:= #0; MessageBeep (1); End End End.