Solved

Button Detection inside an OnExit event of a ttext box

Posted on 2004-04-06
18
389 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
ID: 10769837
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
ID: 10769865
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
ID: 10769890

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

end.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10769954
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
ID: 10769971
ooops, sorry,
but Memo1Exit precedes Button1Click. My example does not work !!!!
0
 
LVL 17

Expert Comment

by:mokule
ID: 10769987
May be
You can set

Memo1.WantTabs := True;

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

Expert Comment

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

Expert Comment

by:esoftbg
ID: 10770083
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
ID: 10770105
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ranunez
ID: 10770464
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
ID: 10770511
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
ID: 10770544
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
ID: 10771718
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
ID: 10771730
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
ID: 10771896
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
ID: 10771977
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
ID: 10772768
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
ID: 10772792
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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 Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

23 Experts available now in Live!

Get 1:1 Help Now