Link to home
Start Free TrialLog in
Avatar of juank
juank

asked on

Which button did I press?

I wanted to know if somebody with a little more than experience and knowledge that me,  can help me with the following thing:

In a form of entrance of data, there is a series of TEdits where numeric values are entered. In one of them, the value is not allowed to be 0 (zero), so in the event TEdit1.OnExit  I wrote a routine so that in case that the value is 0 it appears a MessageDlg with the error. But it happens that if I press the button to Cancel, AND I am in that TEdit, it evaluates the event OnExit and it doesn't cancel until it doesn't put a value <> 0.

Then, like this could solve? Is there a way to  indicate in TEdit1.OnExit that I pressed the button to so it doesn't evaluate the condition?

Thank you and greetings for all those that read this message

Juan Carlos
ASKER CERTIFIED SOLUTION
Avatar of DavidLeeding
DavidLeeding

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Why not check the contents of the important TEdit when the button is pressed?  Just call OnEdit1Exit(self) when Cancel is pressed...

Avatar of marcoszorrilla
marcoszorrilla

You can write a function for testing all the tedits than you want to control and set the KeyPreview of this form to True in order to test if the Esc key is pressed (in the even OnKeyPress from this form, an then call too this function.

Best Regards.
Marcos.
Hi,
If I understand you correctly then try the following:

procedure TForm1.Edit1Exit(Sender: TObject);
begin
 try
  if StrToInt(Edit1.Text) = 0 then
   if MessageDlg('Zero is not allowed', mtError, [mbOk,mbCancel], 0) = mrOk then
     Edit1.SetFocus;
 except
   if MessageDlg(Numeric values only', mtError, [mbOk,mbCancel], 0) = mrOk then
     Edit1.SetFocus;
 end;
end;

Regards, Geo
Avatar of juank

ASKER

It's a good solution. Finally, with my mind a little clear I found a method using the Form1.ActiveControl. So if the ActiveControl is BtnCancel, no validation has to be done.
It works. Anyway thank you