Is there a way to stop a tab switch?

MyForumID

Active member
Joined
Sep 25, 2005
Messages
26
Programming Experience
Beginner
[RESOLVED] Is there a way to stop a tab switch?

Hello,

In my form I have a set of tabs and within a couple of them I have text boxes for input. I've settled on a validation of the input in the box that checks based on the "Leave" function when the user clicks off the box. It's easy to select the box again after the message box using a mybox.select() and to restore the deaulft input and let the user correct it as long as the user isn't clicking on another tab. If he does though - the form switches to the new tab after restoring the default but before the box can be corrected by the user and he has to switch back manually. I tried including a mytab.select() statement in the ""Leave" function, but it's not working.

I'd like to prevent the switching of the tabs if possible. Does anyone have any suggestions please?

Here's an example of my validation code for integers. The first function I found at http://www.kdkeys.net/forums/836/ShowPost.aspx. The second function I figured out:

VB.NET:
    Private Sub MyBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBox.KeyPress
        If Char.IsDigit(e.KeyChar) = False And Char.IsControl(e.KeyChar) = False Then
            e.Handled = True
        End If
    End Sub
    Private Sub MyBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBox.Leave
        If Not IntegerBetween(MyBox.Text, 1, 10) Then
            MyBox.Text = 1
            MyTab.Select()
            MyBox.Select()
        End If
    End Sub

I looked for something similar to the "Handled" function that's present for keypressed as above in the tabs class but couldn't find anything.

Thank you very much.
 
Last edited:
You should not be using the Leave event to validate. Use the Validating event instead:

Private Sub TextBox1_Validating(ByVal sender AsObject, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If TextBox1.Text = "Invalid Text" Then e.Cancel = True
End Sub
 
That worked great - thanks!

I had seen the validating function but didn't know what it did or how it worked. But it does exactly what I wanted it do and it cut my validation code in half since I don't need 2 functions anymore, and it stops the tab switch :)

Thanks again.
 
Back
Top