Author: Tomas Rutkauskas
I'm getting a trouble with an special event inside a OnExit event. I need to know
at OnExit of a TEdit Control, when the user clicks a button such as the Cancel
button. The user may exit the TEdit just with a correct data or when the Cancel
Button was clicked. How can I track this?
Answer:
Assuming that "BlockExit" is a global variable or field of your form:
1 2 procedure TForm1.FormCreate(Sender: TObject);
3 begin4 BlockExit := false;
5 end;
6 7 procedure TForm1.Edit1Exit(Sender: TObject);
8 begin9 BlockExit := (Edit1.Text <> 'OK');
10 end;
11 12 procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
13 begin14 if BlockExit then15 begin16 Beep;
17 Beep;
18 MessageDlg('Wrong data in Edit1', mtError, [mbOK], -1);
19 Edit1.SetFocus;
20 CanClose := false;
21 end22 else23 CanClose := true;
24 end;
25 26 procedure TForm1.btnCancelClick(Sender: TObject);
27 begin28 BlockExit := false;
29 Close;
30 end;
31 32 procedure TForm1.btnOKClick(Sender: TObject);
33 begin34 Close;
35 end;