• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

Button Detection inside an OnExit event of a ttext box

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
ranunez
Asked:
ranunez
  • 6
  • 4
  • 3
  • +3
1 Solution
 
shaneholmesCommented:
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
 
esoftbgCommented:
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
 
shaneholmesCommented:

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

end.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

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

to ranunez
Plaese clarify a little bit.
0
 
esoftbgCommented:
ooops, sorry,
but Memo1Exit precedes Button1Click. My example does not work !!!!
0
 
mokuleCommented:
May be
You can set

Memo1.WantTabs := True;

which prevents You exit from Memo on Tab Key?
0
 
esoftbgCommented:
I don't see a solution at this moment.
emil
0
 
esoftbgCommented:
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
 
esoftbgCommented:
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
 
ranunezAuthor 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.
0
 
shaneholmesCommented:
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
 
mokuleCommented:
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
 
Slick812Commented:
procedure TForm1.Edit1Exit(Sender: TObject);
begin
if not Button1.Focused then  // will allow Button1 to "Ckick" without ShowMessage
ShowMessage('OnExit');
end;
0
 
Slick812Commented:
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
 
esoftbgCommented:
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
 
kretzschmarCommented:
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
 
mokuleCommented:
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
 
kretzschmarCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 6
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now