Conversation

SetFocus not working well

(overview of excerpts from articles in conversation)

Newsgroup: embarcadero.public.delphi.language.delphi.win32

# Lines
wrote on 23-Feb-2012:

I have a form with :
ActiveControl = Txt_Username (TEdit)
procedure TLogin_dlg.FormShow(Sender: TObject);
begin
txt_username.SetFocus;
30
  
Peter Below replied on 23-Feb-2012:

Adalberto Baldini wrote:
  (snip)

You do not need to do that, just set the ActiveControl property of the
  form to this edit control at designtime.
  Windows does not like it when you change the focus in code at a time
  when another focus transition is already in progress. The focus
30
    
Adalberto Baldini replied on 23-Feb-2012:

I did it but not result. Any other suggestion ?
    Adalberto
    
    
    
2
  
Peter Below replied on 23-Feb-2012:

Adalberto Baldini wrote:
  (snip)
This does usually work, so you are doing something in your code that
  causes the focus to be set to another control. Does it end up at a
  consistent location *after the form has displayed*?
  --
17
    
Adalberto Baldini replied on 24-Feb-2012:

Do you mean if I see another object focused?
    I dont see the cursor anywhere.
    There is a way to see which component is focused ? maybe displaying it name
    in a label ?
    Adalberto
7
  
An Pham replied on 24-Feb-2012:

(snip)

Use ActiveControl instead
  from txt_username.SetFocus;
  to ActiveControl := txt_username;
  
  
15
  
Peter Below replied on 24-Feb-2012:

Adalberto Baldini wrote:
  (snip)
Yes.
  (snip)
Mh, the focus may be on a panel or such, which does not show any
  indication of it. Or no control may have the focus, which is highly
  unusual.
41
    
Adalberto Baldini replied on 26-Feb-2012:

I tried TTimer and got
    'No VCL control with focus' when form is displayed , and
    'No focus anywhere' when TEdit got focus.
    Adalberto
    
5
  
Peter Below replied on 26-Feb-2012:

Adalberto Baldini wrote:
  (snip)

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
77
    
Adalberto Baldini replied on 27-Feb-2012:

I modified project creating TLogin_Dld immediately after MainForm creation,
    so when mainfrm event is activate creation of Login_Dlg is done.
    my project :
    begin
    Application.Initialize;
61
  
Peter Below replied on 27-Feb-2012:

Adalberto Baldini wrote:
  (snip)

That is not what I was aiming at. You are still showing a form inside
  the creation sequence of another form, so the problem is basically the
  same as before. What you should do is something like this:
  function LoginUser: boolean;
54
    
Adalberto Baldini replied on 28-Feb-2012:

Peter, yr suggestion works thanks, but explain me why my solution doesn't
    work:
    before Application.Run both forms MainFrm and Login_Dlg are created,
    when Application.Runs MainFrm.OnActivate event is fired consequently after
    creation sequence ?
10
  
Peter Below replied on 28-Feb-2012:

Adalberto Baldini wrote:
  (snip)

Well, "creation sequence" was perhaps not a good choice of term. What
  is also involved is making the forms visible.
  The creation sequence has basically two "branches". The one which is
  clearly evident from the code is the VCL object creation, which you
63
    
Adalberto Baldini replied on 14-Mar-2012:

For another problem, see Application Icon desappear, in Project source I set
    Application.MainFormOnTaskbar := False;
    and this problem desappeared.
    Regards
    Adalberto Baldini
17
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Fri, 28 Jun 2024 09:19:49 UTC
Copyright © 2009-2024
HREF Tools Corp.