locked
focus on this textbox on event of LostFocus RRS feed

  • Question

  • Hi,

    I want to focus on TextBox2 while user is keypressed [tab] in case of (not TextBox2.Text = 'A'),

    But, TextBox3 is having its own checking while Not TextBox3.Text = "A", it will make focusing on TextBox3.

    Finally, It will focus on TextBox3 instead of TextBox2. But, my goal is focus on TextBox2 in this case.

    So, how to make focus on TextBox2 instead of TextBox3 with optimized coding, while keypressed [tab] on TextBox2 and stay on TextBox2 ?

    Private Sub TextBox2_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox2.LostFocus        If Not TextBox2.Text = "A" Then           

    TextBox2.Focus()        End If    End Sub    Private Sub TextBox3_LostFocus(sender As Object, e As System.EventArgs) Handles TextBox3.LostFocus        If Not TextBox3.Text = "A" Then           

    TextBox3.Focus()        End If    End Sub




    Sunday, June 24, 2012 2:24 PM

Answers

  • The LostFocus/GotFocus/Focus events/methods are low level (though ok to use). The documentation says the Enter and Leave events and the Select method should be used.

    If I got you right, you want to keep the focus in Textbox2 if it does not contain an "A". Same for Textbox3. I suggest using the Validating event for this purpose.


    Armin

    Sunday, June 24, 2012 2:54 PM
  • Hi Francis,

    have a look at the sample in the documentation:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx


    Armin


    Sunday, June 24, 2012 3:10 PM
  • I suggest using the Validating event for this purpose.


    Armin

    I agree with Armin.

    The validating event fires whenever the user leaves the control (ergo, they've entered it and now go to move to another control), but unlike the ".Leaving" event, the event args allow you to "lock" your user in by setting e.Cancel = True.

    If it's text, you might consider giving them an "out" by showing an ErrorProvider (it's in your toolbox, just grab it and drag to your form) then show them they need to correct the error -OR- clear the contents entirely in order to proceed. If it's a databound item though, this may not be wise, but it's something to consider.


    Please call me Frank :)

    Sunday, June 24, 2012 4:35 PM

All replies

  • The LostFocus/GotFocus/Focus events/methods are low level (though ok to use). The documentation says the Enter and Leave events and the Select method should be used.

    If I got you right, you want to keep the focus in Textbox2 if it does not contain an "A". Same for Textbox3. I suggest using the Validating event for this purpose.


    Armin

    Sunday, June 24, 2012 2:54 PM
  • Hi Armin Zingler,

    I am newbie on vb.net, please guide me how Validating event works and give me coding example in this case?

    Thank you very much

    Best Regards

    Francis SZE

    Sunday, June 24, 2012 3:05 PM
  • Hi Francis,

    have a look at the sample in the documentation:

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validating.aspx


    Armin


    Sunday, June 24, 2012 3:10 PM
  • I suggest using the Validating event for this purpose.


    Armin

    I agree with Armin.

    The validating event fires whenever the user leaves the control (ergo, they've entered it and now go to move to another control), but unlike the ".Leaving" event, the event args allow you to "lock" your user in by setting e.Cancel = True.

    If it's text, you might consider giving them an "out" by showing an ErrorProvider (it's in your toolbox, just grab it and drag to your form) then show them they need to correct the error -OR- clear the contents entirely in order to proceed. If it's a databound item though, this may not be wise, but it's something to consider.


    Please call me Frank :)

    Sunday, June 24, 2012 4:35 PM
  • Please follow this question, if interested

    http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/2fbaaf57-f82e-490e-a240-0f050db3abf1

    Monday, June 25, 2012 7:28 AM