Results 1 to 11 of 11

Thread: How to set focus to one control when a form pop up?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    40

    Resolved How to set focus to one control when a form pop up?

    hello guys, i want to set focus to a textbox when the form pop up so that the user can enter directly instead of first pointing to the textbox then entering..

    I tried put textbox1.focus() in the form_load event, but it doesn't work. anybody know how to deal with it?

    cheers
    Last edited by sunsunlight; Apr 17th, 2005 at 09:25 PM.

  2. #2
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: How to set focus to one control when a form pop up?

    Try disabling it and enabling it?

  3. #3
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: How to set focus to one control when a form pop up?

    Quote Originally Posted by sunsunlight
    hello guys, i want to set focus to a textbox when the form pop up so that the user can enter directly instead of first pointing to the textbox then entering..

    I tried put textbox1.focus() in the form_load event, but it doesn't work. anybody know how to deal with it?

    cheers
    Code:
    textbox1.select()

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2005
    Posts
    40

    Talking Re: How to set focus to one control when a form pop up?

    textbox1.select!
    it works!
    tks!

  5. #5
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Re: How to set focus to one control when a form pop up?

    If you set the tab order correctly, the first item (0 or 0.0 if using groupboxes) will be the first control to have focus.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  6. #6
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: How to set focus to one control when a form pop up?

    Quote Originally Posted by Ideas Man
    If you set the tab order correctly, the first item (0 or 0.0 if using groupboxes) will be the first control to have focus.

    if your control is in a GroupBox, so the Groupbox it must have a tab index smaller than the controls in it ...

  7. #7
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: How to set focus to one control when a form pop up?

    what is the difference between

    VB Code:
    1. text1.focus
    and

    VB Code:
    1. text1.select

    i have always tried text1.focus , and it didn't work for the first time with the form.

    strange, any1 got explanation ??

    thx

  8. #8
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Re: How to set focus to one control when a form pop up?

    Quote Originally Posted by Zakary
    if your control is in a GroupBox, so the Groupbox it must have a tab index smaller than the controls in it ...
    I didn't quite understand that sorry.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

  9. #9
    Frenzied Member maged's Avatar
    Join Date
    Nov 2002
    Location
    Egypt
    Posts
    1,040

    Re: How to set focus to one control when a form pop up?

    I didn't quite understand that sorry.
    well that means if you have a textbox in a groupbox inside your form.

    you cannot set the tabindex of your textbox to 0 , why ?? because it is contained in a group box that must has a smaller tabindex than the tabindex of it's contained controls.


  10. #10
    New Member
    Join Date
    Apr 2005
    Posts
    1

    Wink Re: How to set focus to one control when a form pop up?

    With textbox, you can use SelectAll() method to select all text in that textbox.

    Code:
    with txt1
    .focus()
    .selectall()
    end with

  11. #11
    Frenzied Member Ideas Man's Avatar
    Join Date
    Aug 2002
    Location
    Australia
    Posts
    1,718

    Re: How to set focus to one control when a form pop up?

    Quote Originally Posted by maged
    well that means if you have a textbox in a groupbox inside your form.

    you cannot set the tabindex of your textbox to 0 , why ?? because it is contained in a group box that must has a smaller tabindex than the tabindex of it's contained controls.
    No, that's not true at all. When you create groupboxes it groups all the controls together, along with their tab orders. You then must specify a "tab order" for the groupboxes, and then the controls inside it.

    The tab order for the groupboxes specifies in what order the groups of controls are selected, and once a groupbox has "focus", it then works in it's internal list of controls and works on the internal tab order of the controls.
    This is why when you have groupboxes, you get a tab order of x.y for controls in a groupbox and x for a groupbox. It's also the reason why it can differentiate between two controls in different groupboxes with the same tab order.

    Please note: If this doesn't make sense I'm sorry, I understand how it works, just not quite how to put it into words lol.
    Last edited by Ideas Man; Apr 21st, 2005 at 02:55 AM.
    I use Microsoft Visual Basic 2005. (Therefore, most code samples I provide will be based around the .NET Framework v2.0, unless otherwise specified)

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