Solved

Which button did I press?

Posted on 2002-07-15
5
174 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now