bokoce bokoce - 3 months ago 28
C# Question

tabcontrol validating when tabpage change

I have tabcontrol with 3 tabpages in it. It always checks the validation when the tabpages of tabcontrol change.
I want to specifically check for validation on a certain tabpage only.

private void registration_tabcontrol_Validating(object sender, CancelEventArgs e)
{
if (!student_firstname_textbox.Text.ToString().Equals(""))
{
DialogResult res = XtraMessageBox.Show("DO YOU WANT TO CANCEL REGISTRATION?", "STUDENT REGISTRATION", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
if (res == DialogResult.Yes)
{
}
else
{
e.Cancel = true;
}
}
}

Answer

Just check for an extra condition in your first if:

tabControl1.SelectedTab == tabPage1 

The above line will check the validation specifically on your tabPage1.

private void registration_tabcontrol_Validating(object sender, CancelEventArgs e)
{
    if (!student_firstname_textbox.Text.ToString().Equals("") && tabControl1.SelectedTab == tabPage1)
    {
        DialogResult res = XtraMessageBox.Show("DO YOU WANT TO CANCEL REGISTRATION?", "STUDENT REGISTRATION", MessageBoxButtons.YesNo, MessageBoxIcon.Stop);
        if (res == DialogResult.Yes)
        {                        
        }
        else
        {
            e.Cancel = true;
        }              
    }  
}

You can also check using:

tabControl1.SelectedIndex == 0   //for tabPage1
Comments