Solved

Button Detection inside an OnExit event of a ttext box

Posted on 2004-04-06
18
386 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.
0
Comment
Question by:ranunez
  • 6
  • 4
  • 3
  • +3
18 Comments
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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.
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility

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

end.
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
to esoftbg
IMHO Your code won't work because Memo1Exit is fired earlier then Button1Click.

to ranunez
Plaese clarify a little bit.
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
ooops, sorry,
but Memo1Exit precedes Button1Click. My example does not work !!!!
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
May be
You can set

Memo1.WantTabs := True;

which prevents You exit from Memo on Tab Key?
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
I don't see a solution at this moment.
emil
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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.
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:ranunez
Comment Utility
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.
0
 
LVL 11

Expert Comment

by:shaneholmes
Comment Utility
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
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
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.
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
procedure TForm1.Edit1Exit(Sender: TObject);
begin
if not Button1.Focused then  // will allow Button1 to "Ckick" without ShowMessage
ShowMessage('OnExit');
end;
0
 
LVL 33

Expert Comment

by:Slick812
Comment Utility
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
0
 
LVL 12

Expert Comment

by:esoftbg
Comment Utility
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 ?
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 250 total points
Comment Utility
as simple a sample

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    procedure Edit1Exit(Sender: TObject);
    procedure Edit2Exit(Sender: TObject);
    procedure Edit3Exit(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Edit1Exit(Sender: TObject);
begin
  if not(activeControl = Button1) then
    edit2.SetFocus;
end;

procedure TForm1.Edit2Exit(Sender: TObject);
begin
  if not(activeControl = Button1) then
    edit3.SetFocus;
end;

procedure TForm1.Edit3Exit(Sender: TObject);
begin
  if not(activeControl = Button1) then
    edit1.SetFocus;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  showmessage('Click');
end;

end.

meikl ;-)
0
 
LVL 17

Expert Comment

by:mokule
Comment Utility
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?
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
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 ;-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

14 Experts available now in Live!

Get 1:1 Help Now