Results 1 to 13 of 13

Thread: [RESOLVED] Set focus

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Posts
    1,031

    Resolved [RESOLVED] Set focus

    I am mystified. On a form I have a text box which I call txtChar
    I want to start the form by placing the flashing cursor in that box to wait for keyboard input.
    After loading the form...
    This line does not work
    Code:
    txtChar.SetFocus
    although it is shown as an option in Intellisense.
    I confirm that the text box properties do include enabled and visible.
    I'm back to vb6 after some time and I must be making a really fundamental mistake...
    Thanks all!

  2. #2
    Addicted Member gilman's Avatar
    Join Date
    Jan 2017
    Location
    Bilbao
    Posts
    136

    Re: Set focus

    I think that the easiest way to start the form with the textbox as the active control is puting his TabIndex property to 0

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Posts
    1,031

    Re: Set focus

    Thanks but no go!

    Maybe it's the surrounding code?
    Code:
    Private Sub Form_Load()
        txtChar.SetFocus
        ...
    End sub
    Thanks all!

  4. #4
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    8,387

    Re: Set focus

    put it in Form_Activate instead of Form_Load (I"ll let you figure out why)
    Sam I am (as well as Confused at times).

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,671

    Re: Set focus

    No, the Activate event can occur many times. Next you'll add some wonky "flag" to try to defeat that.


    To move the focus within the Form it must be visible. To do that during the Load event call the Form's Show method before calling SetFocus on the desired control.

  6. #6
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,270

    Re: Set focus

    To speed up program loading, MS long ago delayed form display until after the Form_Load was complete. Setting the TabIndex of TxtChar to 0 will cause it to take the focus when the form is displayed. Alternately, in the Form_Activate event, define a flag as static.
    Code:
        Static StartFlg As Boolean
        If Not StartFlg Then TxtChar.SetFocus
    I use this technique quite often to do things that affect display on startup.

    J.A. Coutts

  7. #7
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,151

    Re: Set focus

    Yes, I use the Activate event precisely this way all the time as well. A flag like that isn't "wonky". But, be sure to set the flag to True within the Activate event.

    And, the "why" of it is ... on Form_Load, the form is just loaded into memory, not shown yet. So, focus and the like has no meaning at that point. It's only when the form is visible that focus and such has meaning, and it's showing when it's activated.

    Also, maybe every time it's activated, you'd like the focus to go to that control. In that case, you don't need a flag at all.
    Last edited by Elroy; Aug 14th, 2022 at 10:23 AM.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  8. #8
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,671

    Re: Set focus

    There isn't any need for such gymnastics.

    Just call Show before calling SetFocus, typically as the last steps in your Load event handler. All done.

    The "short call" of Show with no arguments passed is the same thing as Visible = True, which will work as well instead.

  9. #9
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    8,387

    Re: Set focus

    Wasn't aware, dile...thanks for pointing it out to me.
    Sam I am (as well as Confused at times).

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2012
    Posts
    1,031

    Re: Set focus

    OK. Just show did it (show.me not necessary). Thanks heaps.
    Also, just having the text box was enough. Set focus not needed anyway!
    Thanks all!

  11. #11
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    8,151

    Re: Set focus

    Quote Originally Posted by Elroy View Post
    Yes, I use the Activate event precisely this way all the time as well. A flag like that isn't "wonky". But, be sure to set the flag to True within the Activate event.

    And, the "why" of it is ... on Form_Load, the form is just loaded into memory, not shown yet. So, focus and the like has no meaning at that point. It's only when the form is visible that focus and such has meaning, and it's showing when it's activated.

    Also, maybe every time it's activated, you'd like the focus to go to that control. In that case, you don't need a flag at all.
    Yeah, actually I was aware of that, but I've just never done it. And yeah, setting the "Visible" flag is just a redundant way of doing the "Show" or "Hide" methods.

    I suppose it's just six-of-one and half-dozen-of-another. To me, it's more intuitive to do it in the Activate event, as I understand that that's when the form has shown with the focus. If I want to do something in that case, but only the first time, I use a flag. Just a personal preference I suppose.
    Any software I post in these forums written by me is provided “AS IS” without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. Please understand that I’ve been programming since the mid-1970s and still have some of that code. My contemporary VB6 project is approaching 1,000 modules. In addition, I have a “VB6 random code folder” that is overflowing. I’ve been at this long enough to truly not know with absolute certainty from whence every single line of my code has come, with much of it coming from programmers under my employ who signed intellectual property transfers. I have not deliberately attempted to remove any licenses and/or attributions from any software. If someone finds that I have inadvertently done so, I sincerely apologize, and, upon notice and reasonable proof, will re-attach those licenses and/or attributions. To all, peace and happiness.

  12. #12
    PowerPoster
    Join Date
    Feb 2006
    Posts
    23,671

    Re: [RESOLVED] Set focus

    There is more to it.

    The "short Show" (and "Hide") calls from within a Form's own event handlers only works at all as a special case. Ever since 32-bit VB4 assigning to the Visible property is preferred and it has fewer side effects.

    From the docs:

    Note When closing a modal form that has been opened from another modal form, the following code worked in previous versions of Visual Basic:
    Code:
    Me.Hide
    Me.Hide ' This now causes an error.
    In current versions of Visual Basic, this code now fails on the second Me.Hide. You can substitute Me.Hide with Me.Visible = False as shown below:
    Code:
    Me.Visible = False
    Me.Visible = False ' No error occurs.
    That may look silly, but the duplicated calls don't have to be adjacent. So now you are back to yet another silly flag to keep track of in order to avoid crashing your programs.

    The old Show/Hide are perfectly safe, but only when the programmer has a clue what he is doing. Visible is far safer if the program might ever be modified by a bathless savage, i.e. Mort the VBArian.

  13. #13
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    8,387

    Re: [RESOLVED] Set focus

    bathless savage

    Now, THAT's a term I may put in my collection!
    Sam I am (as well as Confused at times).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width