Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to recognize Keycode Tab 1

Status
Not open for further replies.

wandan

Technical User
Dec 16, 2001
101
US
Hello,

I am working on a small project where I need to validate that text box has an entry before going to next field. (Note: Validating event does not work here because they have the option to click a different radio button selection.) I have the Enter key working but I cannot seem to get the code to recognize the Tab key. Can you please take a look at the following code and let me know what I am doing wrong? I appreciate any guidance you can provide. THANKS!

Private Sub txtEntry_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtEntry.KeyDown
If e.KeyCode = Keys.Enter Then
'User must enter a value in Entry box if Other or Online is checked
If txtEntry.Text = "" Then
If Me.radOther.Checked Then
MessageBox.Show("Please enter the source of the request before continuing.", "Information Required")
Me.txtEntry.Focus()
ElseIf Me.radOnline.Checked Then
MessageBox.Show("Please enter the RequestID before continuing.", "Information Required")
Me.txtEntry.Focus()
End If
Else
Me.txtReqDate.Focus()
End If
ElseIf e.KeyCode = Keys.Tab Then
If Me.radOther.Checked Then
MessageBox.Show("Please enter the source of the request before continuing.", "Information Required")
Me.txtEntry.Focus()
ElseIf Me.radOnline.Checked Then
MessageBox.Show("Please enter the RequestID before continuing.", "Information Required")
Me.txtEntry.Focus()
End If
End If
End Sub

 
If you set your TextBox to MultiLine = True and AcceptsTab = True, then you can trap the Tab. However, it won't tab to the next control. You can make it do this however. Set MultiLine = True and AcceptsTab = True and see the following example.
Code:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        MessageBox.Show(e.KeyCode.ToString)
        Me.SuspendLayout()
        Me.TextBox1.Enabled = False
        Me.TextBox1.Enabled = True
        Me.ResumeLayout()
    End Sub
 
I do it on Leve Event
Code:
    Private Sub txtEntry_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEntry.Leave
        If txtEntry.Text.Length = 0 Then
            MessageBox.Show("No Text entered")
            txtEntry.Focus()
        End If
    End Sub

Zameer Abdulla
 
Validating event does not work here because they have the option to click a different radio button selection.

Did you set the CausesValidation property of the radio buttons to False?
 
Excellent! Thanks so much!

I set the CausesValidation property of the radio buttons to False and left the Enter key check and the Validation seems to work... one quick follow up...

Is there a trick to not having the validation happen when you close the form (currently I am closing simply using the red X in the corner)?
 
If there are validation errors on your form, the Cancel property in your FormClosing event will be True. In order to bypass validation errors when closing the form, simply set this value to false.
Code:
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = False
    End Sub
 
I tried it but it doesn't work. I am guessing because the validating event happens before the form closing event. It is almost like I need something in the validating event that says set e.false if the form is closing, and I am not at sure that is at all possible.

 
Yes, the Validating event will still fire and you'll get the message box popup, but the window will close after that, correct? I would advise against using an intrusive message box to inform your users of the error. Get aquainted with the ErrorProvider object. Just drag an ErrorProvider onto your form; it will appear in the tray below the form. You can then use it like so:
Code:
    Private Sub txtEntry_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtEntry.Validating
        If (txtEntry.Text.Length = 0) Then
            If (radOnline.Checked) Then
                ErrorProvider1.SetError(sender, "Request Id is required")
            Else
                ErrorProvider1.SetError(sender, "Source of the request is required")
            End If
            e.Cancel = True
        End If
    End Sub

    Private Sub txtEntry_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEntry.Validated
        ErrorProvider1.SetError(sender, String.Empty)
    End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top