Changing Tab Index

I have a form with 4 textboxes.

I use the same keyboard event handler for each textbox.

I want to check the entered data after the <TAB> key is pressed before advancing to the next textbox in the Tab Order.

I have to select the ACCEPTS TAB PROPERTY to TRUE in each textbox to generate a keypressed event when the <TAB> key is pressed.

After the data is validated I want to advance to the next textbox in the Tab Order.

In the event handler, I don't know which textbox is selected.

Is this informantion in the sender part of the Sub statement

If so how do I acess the information

How do I get the current Tab Index

What is the correct syntax for the FOCUS() method

I would like to do something like FOCUS(MyTabindex)




Answer this question

Changing Tab Index

  • pcletson

    the OnLeave event might work, but what you describe is actually the purpose of the Validating event. Take a look at this event:

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

    Hope this helps,
    Adam Braden
    Visual Basic Team


  • TitanIX

    In that case you need to "Cancel" the event. Here's a sample on how to Cancel a Validation/Leaving event:

    Private Function ValidEmailAddress(ByVal emailAddress As String, ByRef errorMessage As String) As Boolean

    ' Confirm there is text in the control.

    If textBox1.Text.Length = 0 Then

    errorMessage = "E-mail address is required."

    Return False

    End If

    ' Confirm that there is an "@" and a "." in the e-mail address, and in the correct order.

    If emailAddress.IndexOf("@") > -1 Then

    If (emailAddress.IndexOf(".", emailAddress.IndexOf("@")) > emailAddress.IndexOf("@")) Then

    errorMessage = ""

    Return True

    End If

    End If

    errorMessage = "E-mail address must be valid e-mail address format." + ControlChars.Cr + _

    "For example 'someone@example.com' "

    Return False

    End Function

    Private Sub textBox1_Validating(ByVal sender As Object, _

    ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating

    Dim errorMsg As String

    If Not ValidEmailAddress(textBox1.Text, errorMsg) Then

    ' Cancel the event and select the text to be corrected by the user.

    e.Cancel = True

    textBox1.Select(0, textBox1.Text.Length)

    ' Set the ErrorProvider error with the text to display.

    MsgBox(errorMsg)

    End If

    End Sub

    Private Sub textBox1_Validated(ByVal sender As Object, _

    ByVal e As System.EventArgs) Handles TextBox1.Validated

    ' If all conditions have been met, clear the error provider of errors.

    MsgBox("E-Mail Address is valid.")

    End Sub

    If the ValidEmailAddress function retuns the value "False" the "e.Cancel" will end the current event, keeping the focus on TextBox1


  • NE Programmer

    If you want to verify the value of the control just before actually advancing to the next control, you might want to use the "OnLeave" procedure of the control in question. If you "Leave" the control (for instance via the <TAB> key) the events mentioned within this procedure will be executed.

    For more info, take a look at the following article: http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.onleave.aspx


  • Bruce Herz

    Hello

    I added the Leave event handler and it works.

    I can validate my data at this time .

    In the case when the data is not valid, how do I keep the <TAB> key from advancing to the next textbox



  • Changing Tab Index