Board index » delphi » OnEnter REALLY doesn't work!!!!

OnEnter REALLY doesn't work!!!!

Here's one that's been bugging me...  Why doesn't the OnEnter event fire
when it is the NextControl; i.e. I'm setting
ActiveControl:=NextControl.  The control does NOT get focus, that is
there is no cursor and no text selected, but the user can still start
typing right away in the field.  If I TAB, then Back-TAB, the field gets
focus fine.  Can someone please explain what I might need to do to get
the OnEnter event to fire!!!!

Thanks for any help!!!
--
Bruce Goldstein
Software Engineer
Interactive Strategies, Inc.
br...@centcon.com

 

Re:OnEnter REALLY doesn't work!!!!


On 17/09/1996 00:59, in message <323DDBC4.1...@centcon.com>, "Bruce C.

Quote
Goldstein" <br...@centcon.com> wrote:
> ActiveControl:=NextControl.  The control does NOT get focus, that is
> there is no cursor and no text selected, but the user can still start

Don't assign the NextControl to CurrentControl. Use either SetFocus, or the
code I documented elsewhere...

Oh yes...and OnEnter is supposed to fire when you Enter the control by standard
means, eg. Mouseclick in the Control, or Tab, or SetFocus.

Stefan

Re:OnEnter REALLY doesn't work!!!!


Quote

> Why don't you use NextControl.setfocus ?

> Jethro Leung

That didn't help either....same problem.
--
Bruce Goldstein
Software Engineer
Interactive Strategies, Inc.
br...@centcon.com

Re:OnEnter REALLY doesn't work!!!!


Quote
Bruce C. Goldstein wrote:

> Here's one that's been bugging me...  Why doesn't the OnEnter event fire
> when it is the NextControl; i.e. I'm setting
> ActiveControl:=NextControl.  The control does NOT get focus, that is
> there is no cursor and no text selected, but the user can still start
> typing right away in the field.  If I TAB, then Back-TAB, the field gets
> focus fine.  Can someone please explain what I might need to do to get
> the OnEnter event to fire!!!!

> Thanks for any help!!!
> --
> Bruce Goldstein
> Software Engineer
> Interactive Strategies, Inc.
> br...@centcon.com

Why don't you use NextControl.setfocus ?

Jethro Leung

Re:OnEnter REALLY doesn't work!!!!


On Tue, 17 Sep 1996 16:51:09 -0700, "Bruce C. Goldstein"
<br...@centcon.com> you smacked the keyboard repeatedly to write:

Quote

>> Why don't you use NextControl.setfocus ?

>> Jethro Leung

>That didn't help either....same problem.
>--
>Bruce Goldstein
>Software Engineer
>Interactive Strategies, Inc.
>br...@centcon.com

I missed the first part of this, but if you are trying to set focus to
the next control when someone presses enter you can use this:

procedure TCarCheckInForm.UnitNoKeyPress(Sender: TObject; var Key:
Char);
begin
   if Key = #13 then
      begin
         Key := #0;
         Perform(WM_NextDlgCtl,0,0);
      end;
end;

Brien King
bk...@primenet.com

Re:OnEnter REALLY doesn't work!!!!


On 18/09/1996 10:53, in message <323fbb76.34947818@news>, Brien King

Quote
<bk...@primenet.com> wrote:
> On Tue, 17 Sep 1996 16:51:09 -0700, "Bruce C. Goldstein"
> <br...@centcon.com> you smacked the keyboard repeatedly to write:

Why use the Windows API call when you just dig around in the Component Writer's
Guide and use the native Delphi Call:

procedure TCarCheckInForm.UnitNoKeyPress(Sender: TObject; var Key: Char);
begin
   if Key = #13 then
      begin
         Key := #0;
         SelectNext(Sender as TWinControl,True,True);
      end;
end;

Quote
> procedure TCarCheckInForm.UnitNoKeyPress(Sender: TObject; var Key:
> Char);
> begin
>    if Key = #13 then
>       begin
>          Key := #0;
>          Perform(WM_NextDlgCtl,0,0);
>       end;
> end;

Stefan

Re:OnEnter REALLY doesn't work!!!!


On a related matter...  When I click on a text box, I like for the entire
text in the box to become highlighted.  So, I tried setting the SelStart and
SelLength properties accordingly in the OnEnter event, but it does not work!?

Quite odd.  If anyone has an explanation, I'd be quite happy to hear of it!
:>

Alan Krause

Re:OnEnter REALLY doesn't work!!!!


On Wed, 18 Sep 96 13:42:06 GMT, kraus...@csusm.edu (Alan Krause) you
smacked the keyboard repeatedly to write:

Quote
>On a related matter...  When I click on a text box, I like for the entire
>text in the box to become highlighted.  So, I tried setting the SelStart and
>SelLength properties accordingly in the OnEnter event, but it does not work!?

>Quite odd.  If anyone has an explanation, I'd be quite happy to hear of it!
>:>

>Alan Krause

Show us the code that does the selection.

Brien King
bk...@primenet.com

Re:OnEnter REALLY doesn't work!!!!


Quote
In article <32407f36.12319121@news>, bk...@primenet.com (Brien King) wrote:
>Show us the code that does the selection.

Sure...

Start a new project.  Create two Edit boxes (named Edit1 and Edit2 oddly
enough).  For both of these Edit boxes, set the AutoSelect property to
False - this way we may be sure that any selection which is done
automatically was done by the program.

Now, let's create an event-handler for Edit2's OnEnter event.  Select
Edit2, then double click on the OnEnter entry of the Events page of the
Object Inspector.  Delphi creates the following procedure:

procedure TForm1.Edit2Enter(Sender: TObject);
begin
end;

Now, add the following code to the procedure between the begin..end block:

   with Sender as TEdit do begin
      SelStart := 0;
      SelLength := Length(Text);
   end;

Compile and run the program.  Click on Edit2 and notice that it does NOT
select the entire text.  Now, hit the [TAB] key to move to Edit1, then
hit the [TAB] key to move back to Edit2, and note that it DOES select
the entire text when you enter the control via a TAB key.

All of the above was done with Delphi 1.0 on a Win 3.1 platform.

Alan Krause

Re:OnEnter REALLY doesn't work!!!!


On Wed, 18 Sep 96 16:31:44 GMT, kraus...@csusm.edu (Alan Krause) you
smacked the keyboard repeatedly to write:

[Snip]

Quote
>Now, add the following code to the procedure between the begin..end block:

>   with Sender as TEdit do begin
>      SelStart := 0;
>      SelLength := Length(Text);
>   end;

>Compile and run the program.  Click on Edit2 and notice that it does NOT
>select the entire text.  Now, hit the [TAB] key to move to Edit1, then
>hit the [TAB] key to move back to Edit2, and note that it DOES select
>the entire text when you enter the control via a TAB key.

That is what is supposed to happen.

You override the OnEnter event, but leave the OnClick event alone, so
here is what happens:

1) You click on the Edit control
2) An OnEnter message is sent and your code in the OnEnter event is
called
3) The entire text is selected
3) An OnClick message is sent and the default code takes over
4) The cursor moves to the position you clicked on.

when you tab to the control, you only do 2 & 3 since the mouse is not
involved.

What you need to do, is either set the AutoSelect on, or if you don't
want the entire text selected, maybe just a portion, then you should
do the following:

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    procedure Edit2Enter(Sender: TObject);
    procedure Edit2Click(Sender: TObject);
  private
    { Private declarations }

        {This is a flag that allows you to select portions of the text
        the second time you click on the edit box as long as you haven't
        exited the control.  Otherwise the routine would continually select
        the entire text.}
     SecondEdit2: boolean;
  public
    { Public declarations }
    MyForm1, MyForm2: TForm;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Edit2Enter(Sender: TObject);
begin
   SecondEdit2 := False;
end;

procedure TForm1.Edit2Click(Sender: TObject);
begin
   if not SecondEdit2 then
      begin
         with Sender as TEdit do
            begin
               SecondEdit2:= True;
               SelStart := 0;
               SelLength := Length(Text);
            end;
      end;
end;

So the OnEnter does work, its just a matter of watching what messages
get handled and which ones don't.  This type of thing has bitten me
before as well.  Its also important to watch the order in which events
occur so you don't accidently create the chicken before the egg :-)

Hope this helps...

Brien King
bk...@primenet.com

Re:OnEnter REALLY doesn't work!!!!


Quote
In article <3240ccac.1038437@news>, bk...@primenet.com (Brien King) wrote:
>You override the OnEnter event, but leave the OnClick event alone, so
>here is what happens:

Hmmm.  So the OnEnter even handler is NOT equivalent to VB's GotFocus event
handler...  They're just a bit different in that respect (as well as many
others, of course!), I guess.

Quote
>What you need to do, is either set the AutoSelect on, or if you don't

However, making AutoSelect True does NOT highlight the text when you first
click in an edit box...  which, IMHO, it should - but it doesn't.
Quote
>want the entire text selected, maybe just a portion, then you should
>do the following:

Other Threads