Validation & Tab Control

I have a custom control in which I want to perform some data validation in the OnValidating event. If the data is invalid, the control sets the CancelEventArgs's Cancel property to true to reset the focus back to the control.

This all works fine until I host this control on a tab page. If the user enters invalid data into the control then directly clicks on another tab, the validating event gets fired several times in succession, and the tab page changes anyway.

Is there a way to prevent the tab page from being changed in this secnario


Answer this question

Validation & Tab Control

  • nsimeonov

    Can you post your code for your OnValidating handler   I created a simple example and it worked just fine:

    Public Class MyTextbox
        Inherits TextBox

        Protected Overrides Sub OnValidating(ByVal e As System.ComponentModel.CancelEventArgs)
            e.Cancel = True
            MyBase.OnValidating(e)
        End Sub
    End Class

  • Miles Cohen

    You could reset the TabPage in the TabControls SelectedIndexChanged event.

    vb example..

    Private Sub TabControl1_SelectedIndexChanged(...)...
        If somebooleanvariable = False Then
            TabControl1.SelectedTab = TabPage1
        End If
    End Sub


  • Validation & Tab Control