Solved

Which button did I press?

Posted on 2002-07-15
5
180 Views
Last Modified: 2010-04-04
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
0
Comment
Question by:juank
5 Comments
 
LVL 1

Accepted Solution

by:
DavidLeeding earned 100 total points
ID: 7155851
Hi

Try using the "Focused" function of your Cancel button as part of your OnExit code. Eg:

procedure TForm1.Edit1Exit(Sender: TObject);
begin

  // check whether the cancel button is focused
  if not Button1.Focused then  

    // it's not, so do validation stuff:
    if StrToIntDef(Edit1.Text, 0) = 0 then
    begin
      MessageDlg('Zero is not allowed', mtError, [mbOk], 0);
      Edit1.SetFocus
    end;

end;
0
 
LVL 2

Expert Comment

by:mikepj
ID: 7155915
Why not check the contents of the important TEdit when the button is pressed?  Just call OnEdit1Exit(self) when Cancel is pressed...

0
 
LVL 3

Expert Comment

by:marcoszorrilla
ID: 7156350
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.
0
 
LVL 17

Expert Comment

by:geobul
ID: 7156555
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
0
 

Author Comment

by:juank
ID: 7158021
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question