使用tabcontrol切换选项卡时如何检查文本框中的数据

时间:2014-02-13 00:49:45

标签: vb.net validation textbox tabcontrol

当我在tabcontrol上切换标签时,我正在寻找一种检查数据是否输入2个文本框的方法。

我也想知道如何使用按钮指定单击时要转到的选项卡。

我尝试使用tabcontrol1.click设置文本框检查但是当您单击任何选项卡时它会使警告消失,即使只是尝试返回输入数据。

- 感谢您的帮助!

    Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
    If fNameTxtBox.Text = "" Or
        IDTxtBox.Text = "" Then
        MessageBox.Show("You must enter a Name and ID number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        fNameTxtBox.Focus()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个

Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
    If fNameTxtBox.Text = "" Or
     IDTxtBox.Text = "" Then
        MessageBox.Show("You must enter a Name and ID number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
        e.Cancel = True '<<<< were telling VB that the event shoulb be terminated
        fNameTxtBox.Focus()
    End If

End Sub

'For Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If fNameTxtBox.Text = "" Or
      IDTxtBox.Text = "" Then
        MessageBox.Show("You must enter a Name and ID number.", "Data Entry Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)

        'Choose one here
        TabControl1.SelectedTab = TabPage1 'Assuming TabPage1 is the tab where the fNameTxtBox is resides
        TabControl1.SelectedIndex = 0 'Assuming the index number of the TabPage in the tab control where the fNameTxtBox is resides

        fNameTxtBox.Focus()
    End If

End Sub