locked
Why Setfocus is not working in my file? RRS feed

  • Question

  • I do not know why the set focus is not working in this file, I removed all controls that may hinder the focus but still not focusing when userform intializes. File
    Sunday, January 5, 2014 3:01 PM

Answers

  • Don't use code. Simply set the TabIndex property of the HC text box to 0 in the Visual Basic Editor.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Ahmed Morsyy Sunday, January 5, 2014 4:25 PM
    Sunday, January 5, 2014 3:15 PM
  • Don't set focus and it will work... just delete your form initialize routine

    It already has focus if tabindex is 0.

    Probably can't set focus anyway until after initialisation is complete?

    This will also work:

    Private Sub UserForm_Activate()
     HC.SetFocus
    End Sub

    In my testing, setting focus during Form_Initialize causes a propblem so the above event is better... unless the control in question already has tabindex 0.  Then don't need to setfocus.

    This is also quite cool...

    Private Sub HC_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    HC.SetFocus
    End Sub
    


    • Marked as answer by Ahmed Morsyy Sunday, January 5, 2014 4:25 PM
    • Edited by Cool.Blue Sunday, January 5, 2014 4:28 PM
    Sunday, January 5, 2014 4:15 PM

All replies

  • Don't use code. Simply set the TabIndex property of the HC text box to 0 in the Visual Basic Editor.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Marked as answer by Ahmed Morsyy Sunday, January 5, 2014 4:25 PM
    Sunday, January 5, 2014 3:15 PM
  • Thanks Hans, but when I add another textbox, HC2, and I want to initialize with HC2, 

    Private Sub UserForm_Initialize()
    Me.HC2.SetFocus
    End Sub

    still not focusing to HC2. Why?

    • Proposed as answer by Cool.Blue Sunday, January 5, 2014 4:14 PM
    • Unproposed as answer by Cool.Blue Sunday, January 5, 2014 4:14 PM
    Sunday, January 5, 2014 3:29 PM
  • Set the TabStop property to 0 for the text box that should have the initial focus.

    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Proposed as answer by Cool.Blue Sunday, January 5, 2014 4:33 PM
    Sunday, January 5, 2014 3:52 PM
  • I'm sorry, nothing changed. TabStop: True or false, both did not affect the focus.
    Sunday, January 5, 2014 4:13 PM
  • Don't set focus and it will work... just delete your form initialize routine

    It already has focus if tabindex is 0.

    Probably can't set focus anyway until after initialisation is complete?

    This will also work:

    Private Sub UserForm_Activate()
     HC.SetFocus
    End Sub

    In my testing, setting focus during Form_Initialize causes a propblem so the above event is better... unless the control in question already has tabindex 0.  Then don't need to setfocus.

    This is also quite cool...

    Private Sub HC_MouseMove(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    HC.SetFocus
    End Sub
    


    • Marked as answer by Ahmed Morsyy Sunday, January 5, 2014 4:25 PM
    • Edited by Cool.Blue Sunday, January 5, 2014 4:28 PM
    Sunday, January 5, 2014 4:15 PM
  • Thank you Jacophile and Hans. It is working know. I should set TabIndex to 0 for the txetbox that I need to start with.
    Sunday, January 5, 2014 4:22 PM