We help IT Professionals succeed at work.

Button Detection inside an OnExit event of a ttext box

ranunez
ranunez asked
on
Medium Priority
460 Views
Last Modified: 2010-04-05
I have an OnExit event for a Text Box.  I need to react to the click of a button as opposed to a tab exit.  For example if I click on a save button I want to be able to program a different repsonse.

  My Text Box is on a panel and my button is on a different panel.
Comment
Watch Question

Im not folowing you - You exit the Text box from a click of the button? And its not surfacing the OnExit event?

You can always call the OnExit event yourself

 MyEdit.OnExit(Self);

SHane

Commented:
unit Unit_Q_20945682;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TExitMode = (emNone, emButton);
  TForm1 = class(TForm)
    Panel1: TPanel;
    Memo1: TMemo;
    Panel2: TPanel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Memo1Exit(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private { Private declarations }
  public  { Public declarations }
    ExitMode: TExitMode;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ExitMode := emNone;
end;

procedure TForm1.Memo1Exit(Sender: TObject);
begin
  case ExitMode of
    emButton:
    begin
      // your code here
    end;
    else
    begin
      // your code here
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExitMode := emButton;
end;

end.

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExitMode := emButton;
  Memo1.OnExit(Self);
end;

end.

Commented:
to esoftbg
IMHO Your code won't work because Memo1Exit is fired earlier then Button1Click.

to ranunez
Plaese clarify a little bit.

Commented:
ooops, sorry,
but Memo1Exit precedes Button1Click. My example does not work !!!!

Commented:
May be
You can set

Memo1.WantTabs := True;

which prevents You exit from Memo on Tab Key?

Commented:
I don't see a solution at this moment.
emil

Commented:
May be this is a solution, I am very tired, because here is 0:48 am. Bye....

unit Unit_Q_20945682;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TExitMode = (emNone, emButton, emTabKey);
  TForm1 = class(TForm)
    Panel1: TPanel;
    Memo1: TMemo;
    Panel2: TPanel;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Memo1Exit(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
  private { Private declarations }
  public  { Public declarations }
    ExitMode: TExitMode;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  ExitMode := emNone;
end;

procedure TForm1.Memo1Exit(Sender: TObject);
begin
  case ExitMode of
    emTabKey:
    begin
      ShowMessage('Exit by Tab Key');
      // your code here
    end;
    else
    begin
      ShowMessage('Exit by Button Click');
      // your code here
    end;
  end;
  ExitMode := emNone;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ExitMode := emButton;
  Memo1Exit(Self);
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if (Key=#9) then
  begin
    Key := #0;
    ExitMode := emTabKey;
    Memo1Exit(Self);
  end;
end;

end.

Commented:
Thanks mokule,
>May be
>You can set
>Memo1.WantTabs := True;
>which prevents You exit from Memo on Tab Key?

I used that in above example. That's important,
emil

Author

Commented:
Here is the real life expample

  Whenever a customers add a new record a SetFocus is used to activate the EditBox to start edit from.  Since only certain EditBoxes need to be accessed when adding a record some field have an OnExit event to SetFocue to another EditBox when adding a record.

  When the user clicks on the SaveButton I want to Abort the process and Save the Edits.  The problem is my SaveButtonOnClick event will never occur, because the OnExit occurs first and the use is moved to the next box in the chain.
How about testing a variable

var
 SaveMode: Boolean = False;

Procedure Edit1Exit(Sender: TObject)
begin
 if not SaveMode then
 begin
  //perform your save
 end
 else
 //maybe return focus to edit field -  Edit1.SetFocus;
end;


Procedure MyButtonClick(Sender: TObject)
begin
 SaveMode:= True;
end;


Shane

Commented:
I suggest such solution
Remove Your OnExit procedures from controls and
either

disable some controls

or

set TabOrder property for controls to achieve desired TabOrder chain.

Commented:
procedure TForm1.Edit1Exit(Sender: TObject);
begin
if not Button1.Focused then  // will allow Button1 to "Ckick" without ShowMessage
ShowMessage('OnExit');
end;

Commented:
Oh, It may be helpful, A TSpeedButton  Does NOT  get the  "Focus"  so if you use a TSpeedButton instead of a TButton, you can click it and There will Be  NO OnExit  event in the Edit,  because the Edit  will still have focus

Commented:
Hi ranunez,
What is more important to know:
1. about Tab-key is pressed into Edit1 ? Because Edit1 can lost the focus by clicking other Edit control without pressing Tab-key and without clicking on the SaveButton....
2. about SaveButton is clicked when Edit1 is focused ?
Top Expert 2004
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Commented:
Hi guys,
Please note that solution with OnExit event destroys normal bevaviour TAB-key goes to "next" Control and SHIFT+TAB key goes to "previous" control.
What about the user who wants to go to previous control using a keybord.
My solution is free from this disadvantage. That is why I proposed other solutio than OnExit coding.

Question to ranunez

What is Your desired behaviour after pressing SHIFT+TAB?
Top Expert 2004

Commented:
mokule,
this would be the default windows bahaviour
and it may be wanted to do it in another way.

clarification can only give the questioner

meikl ;-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.