Results 1 to 9 of 9

Thread: Set Focus in Textbox

  1. #1

    Thread Starter
    Addicted Member MikeHost's Avatar
    Join Date
    Nov 2000
    Location
    Missouri
    Posts
    175

    Set Focus in Textbox

    How do you set focus on a textbox???? Seems like it should be easy, but...
    <<<<>>>>

  2. #2
    Hyperactive Member
    Join Date
    Feb 2001
    Location
    Houston, TX
    Posts
    342
    VB Code:
    1. Textbox1.Focus
    Make sure the form is visible before executing the method.

  3. #3
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    is the form visible on the page load event?

  4. #4
    Hyperactive Member Bananafish's Avatar
    Join Date
    Jan 2001
    Posts
    394
    Ive found occasional problems with setting focus in the form_load event.

    To get around this I have processed the set focus from a timer that is enabled by the form_load event and dis-enabled after it processes once.

  5. #5
    Lively Member
    Join Date
    Jan 2002
    Posts
    105
    I use this event to set the focus to a textbox on the form load. It seems to work ok for me.

    Code:
     Protected Overrides Sub OnActivated(ByVal e As System.EventArgs)
    
            If (Me.txtDataFormat.Enabled = True) Then
                'Set the focus to the txtDataFormat textbox otherwise
                'set it to the txtDisplay textbox
                Me.txtDataFormat.Focus()
            Else
                'Set the focus to the txtDisplay textbox
                Me.txtDisplay.Focus()
            End If
    
        End Sub

  6. #6
    Member
    Join Date
    Jan 2003
    Location
    Earth
    Posts
    60
    txt1.focus()
    its easier and faster
    i will be come a better programmer some day! thanks to vbforums.com

  7. #7
    Frenzied Member EyeTalion's Avatar
    Join Date
    Jul 2000
    Location
    New York
    Posts
    1,075
    yeah, it is quicker but it doesn't work on the load of the page...you have to put txt1.focus in the code where you call the new page from...put it after frm1.show...

  8. #8
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277
    You could always set the text box to be the first item in the tab order, and then it'll have focus automatically when the form starts.
    ~Peter


  9. #9
    Junior Member
    Join Date
    Feb 2004
    Location
    Cape Town, South Africa
    Posts
    18
    This only happens in VS.net 2002, it is fixed in vs.net 2003.

    Took me a few days to discover !!


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