Mirror

How to track a TEdit at an OnExit event (Views: 102)


Problem/Question/Abstract:

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:

procedure TForm1.FormCreate(Sender: TObject);
begin
  BlockExit := false;
end;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  BlockExit := (Edit1.Text <> 'OK');
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if BlockExit then
  begin
    Beep;
    Beep;
    MessageDlg('Wrong data in Edit1', mtError, [mbOK], -1);
    Edit1.SetFocus;
    CanClose := false;
  end
  else
    CanClose := true;
end;

procedure TForm1.btnCancelClick(Sender: TObject);
begin
  BlockExit := false;
  Close;
end;

procedure TForm1.btnOKClick(Sender: TObject);
begin
  Close;
end;

<< Back to main page