Question

I am looking for a way to check if data is entered in 2 textboxes when I switch tabs on a tabcontrol.

I am also wondering how I can use a button to specify what tab to go to on a click.

I tried using the tabcontrol1.click to setup the textbox check but it makes the warning go off when you click on any tab even just trying to go back to enter the data.

-thanks for the help!

    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
Was it helpful?

Solution

Try this

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top