none
Control Focus / Control Select RRS feed

Answers

  • Hi dbasnett,
    In the Form.Load event, Control.Select() could work but Control.Focus() could not. However, if you put Control.Focus() in the Form.Shown event then it works.

    Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Load
    MyControl.Focus()
    End Sub

    does not work, but:

    Private Sub Form_Shown(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Shown
    MyControl.Focus()
    End Sub

    does work.

    In both event handlers, MyControl.Select() works quite happily.

    Here is also a simple example:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.TextBox1.Text = "test"       
            Me.TextBox1.Select()
     End Sub
    Here the focus is set on TextBox1.
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.TextBox1.Text = "test"
            'Me.show()      
            Me.TextBox1.focus()
     End Sub
    Here if we comment this line Me.show(), the focus is not set on the TextBox1.

    The two methods are implemented differently. If you want to understand in deep,I recommend that you get hold of Lutz Roeder's Reflector. You can get it from:
    http://www.aisto.com/roeder/dotnet/
    It is invaluable for developing into the internals of the Framework. Hope it helps.

    Best regards,

    Liliane

     


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Tuesday, June 29, 2010 6:29 AM
    Tuesday, June 8, 2010 5:22 AM
  • I read the the MSDN Documentation often.  "Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms."

    Why, as an application programmer, should I not use .Focus?


    Subnet Calculator / Planner      Serial Port


    Yes therfore I gave such a stupid answer, everybode can write something, some persons have dreams and write than down what they thought.

    Can you give any represatative qualified link around your question.

    There are many methods/properties which do the same.

    You can ask the same about the Hide/Show method and the property Visible.

    One is a property which calls the hide or show method and the other a methods, which sets the visible property to true or false.

    There will be a time difference, but proabley impossible to measure. In my idea just a matter of preference like we see often.

    (Also Microsoft can change the way the do it in every framework, as long as the result is exactly the same)

    But if you have qualified documentation, than I gladly like to see that.

    By the way, this is easy testable by yourself. Simply a form with 2 textboxes on a form a stopwatch and then a loop which does n times the two methods until it is visible

     


    Success
    Cor
    • Marked as answer by Liliane Teng Tuesday, June 29, 2010 6:31 AM
    Monday, June 7, 2010 11:19 AM

All replies

  • Hello dbasnett,
    Thanks for your post.
    Select() will not give the control the focus if the container doesn't have the focus or is not visible. Focus() will give the container the focus but only if that is possible (i.e. not disabled and not invisible). You'd normally use Focus() but should use Select() when Focus() can fail. For example, in the Load event (form is not visible) or when a modal dialog is shown (other forms are disabled). Use Control.CanFocus in case of doubt.

    For more information:
    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx
    (Control.Focus Method)
    http://msdn.microsoft.com/en-us/library/7wt11hea.aspx
    (Control.Select Method)

    Hope it helps.

    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Monday, June 7, 2010 6:06 AM
  • @Liliane - The question comes from the MSDN docs for Focus.  I was asking for a little more detail on what it meant.  For you to mark Cor's reply as the answer is absurd.  I unmarked it.  Hey, thanks though for bumping the question up.

    Subnet Calculator / Planner      Serial Port
    Monday, June 7, 2010 10:14 AM
  • Hello again dbasnett,

    I am sorry. I could understand your problem. After you see my above reply, do you have a more clearer concept about the two methods? Is it helpful?

    If you have any problems, please feel free to follow up. Thanks.

    Best regards,
    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Monday, June 7, 2010 10:26 AM
  • I read the the MSDN Documentation often.  "Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms."

    Why, as an application programmer, should I not use .Focus?


    Subnet Calculator / Planner      Serial Port
    Monday, June 7, 2010 10:31 AM
  • I read the the MSDN Documentation often.  "Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms."

    Why, as an application programmer, should I not use .Focus?


    Subnet Calculator / Planner      Serial Port


    Yes therfore I gave such a stupid answer, everybode can write something, some persons have dreams and write than down what they thought.

    Can you give any represatative qualified link around your question.

    There are many methods/properties which do the same.

    You can ask the same about the Hide/Show method and the property Visible.

    One is a property which calls the hide or show method and the other a methods, which sets the visible property to true or false.

    There will be a time difference, but proabley impossible to measure. In my idea just a matter of preference like we see often.

    (Also Microsoft can change the way the do it in every framework, as long as the result is exactly the same)

    But if you have qualified documentation, than I gladly like to see that.

    By the way, this is easy testable by yourself. Simply a form with 2 textboxes on a form a stopwatch and then a loop which does n times the two methods until it is visible

     


    Success
    Cor
    • Marked as answer by Liliane Teng Tuesday, June 29, 2010 6:31 AM
    Monday, June 7, 2010 11:19 AM
  • One from Poster Liliane - http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx

    You might have to scroll down to see the light yellow note box.


    Subnet Calculator / Planner      Serial Port
    Monday, June 7, 2010 10:16 PM
  • Hi dbasnett,
    In the Form.Load event, Control.Select() could work but Control.Focus() could not. However, if you put Control.Focus() in the Form.Shown event then it works.

    Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Load
    MyControl.Focus()
    End Sub

    does not work, but:

    Private Sub Form_Shown(ByVal sender As Object, ByVal e As EventArgs)
    Handles MyBase.Shown
    MyControl.Focus()
    End Sub

    does work.

    In both event handlers, MyControl.Select() works quite happily.

    Here is also a simple example:
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.TextBox1.Text = "test"       
            Me.TextBox1.Select()
     End Sub
    Here the focus is set on TextBox1.
     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.TextBox1.Text = "test"
            'Me.show()      
            Me.TextBox1.focus()
     End Sub
    Here if we comment this line Me.show(), the focus is not set on the TextBox1.

    The two methods are implemented differently. If you want to understand in deep,I recommend that you get hold of Lutz Roeder's Reflector. You can get it from:
    http://www.aisto.com/roeder/dotnet/
    It is invaluable for developing into the internals of the Framework. Hope it helps.

    Best regards,

    Liliane

     


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    • Marked as answer by Liliane Teng Tuesday, June 29, 2010 6:29 AM
    Tuesday, June 8, 2010 5:22 AM
  • One from Poster Liliane - http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx

    You might have to scroll down to see the light yellow note box.


    Subnet Calculator / Planner      Serial Port


    Yes strange first time I see this, I've redirected your question, also I don't understand why WPF has a focus method but no Select method.

    http://msdn.microsoft.com/en-us/library/ms590082(v=VS.100).aspx

    However, so far I keep it to my first reply to you.

    Somebody has asked why there are two methods, and then an answer is easily invented.

     

     


    Success
    Cor
    Tuesday, June 8, 2010 5:40 AM
  • Liliane,

    What you write is in my idea correct.

    However can you keep this thread a while open, I've redirected the question to get more answers.

    The sentence is strange in my idea not that it is not different working, but be then consequent and don't create in WPF again a select method and not as focus method.

    Cor

    Tuesday, June 8, 2010 5:50 AM
  • OK, Cor. Thanks for your contribution to the forum. Hope this question could get more answers.

    Have a nice day. :)

    Best regards,

    Liliane


    Please mark the replies as answers if they help and unmark them if they provide no help. Thanks
    Tuesday, June 8, 2010 6:11 AM
  • I am simply asking for a clarification of this statement made by Microsoft:

    "Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms."

     

     


    Subnet Calculator / Planner      Serial Port
    Tuesday, June 8, 2010 11:46 AM
  • I am simply asking for a clarification of this statement made by Microsoft:

    "Focus is a low-level method intended primarily for custom control authors. Instead, application programmers should use the Select method or the ActiveControl property for child controls, or the Activate method for forms."

     

     


    Subnet Calculator / Planner      Serial Port


    Did you read my reply to the message in with you showed the link?

    Or do you have an explaination?


    Success
    Cor
    Tuesday, June 8, 2010 12:11 PM