Article

From:
To:
Adalberto Baldini
Subject:
Re: SetFocus not working well
Newsgroup:
embarcadero.public.delphi.language.delphi.win32

Re: SetFocus not working well

Adalberto Baldini wrote:

> I tried TTimer and got 
> 'No VCL control with focus' when form is displayed , and
> 'No focus anywhere' when TEdit got focus.
> 
> Adalberto

Weird. Your code (or a component you are using) does something to
seriously screw up the Windows' focus handling, obviously. I think the
whole problem is that you are creating this login form from the
OnActivate event of the main form, that is: inside the creation
sequence for the main form.

As an aside: a better way to handle login to your application is to create and show the login form *before* the main form has even been created (but after any datamodules for the database you want to login to have been created). This requires a small modification of the DPR file, but is otherwise neater to do, and you can just terminate the app if login fails without the usually time-consuming task of creating the main form.
Anyway, the usual cure for focus problems is to delay the setfocus call until the code flow has returned to the message loop. You do that by posting a custom message to the form (in this case the login form) from the location you are now having the SetFocus call in. Then set the focus in the message handler for the custom message.
{code: Delphi} const   UM_SETFOCUS = WM_USER +444; type   TUmSetFocus = record     msg: Cardinal;     unused: WPARAM;     Ctrl: TWinControl;     Result: LRESULT;   end;
  TLogin_Dlg= class(TForm)     ....   protected     procedure UMSetFocus(var Message: TUMSetFocus); message UM_SETFOCUS;

procedure TLogin_Dlg.UMSetFocus(var Message: TUMSetFocus); begin   Message.Ctrl.SetFocus end;
And inside the OnCreate event handler
  PostMessage(Handle, UM_SETFOCUS, 0, LPARAM(txt_username));
-- Peter Below (TeamB) Don't be a vampire (http://slash7.com/pages/vampires), use the newsgroup archives : http://codenewsfast.com http://groups.google.com
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Sun, 30 Jun 2024 18:16:49 UTC
Copyright © 2009-2024
HREF Tools Corp.