Remaining Tab Management

I have a tab control and 3 tabs. (WITH#)

if I am on tab 2 and edit the value of the text field and then click the tab 3, I need to check what was entered in the text field. if it’s right, I have to let me switch to tab 3, the rest should stay on tab 2 how can I achieve this?

iam, processing the leave event on tab2, i check the value of the text field there, and if it is found invalid i is set as tabcontrol.Selectedtab = tabpage2; check does it, but switch to a new tab! how can i limit navigation.

I'm new to C #, so maybe I'm dealing with the wrong event!

Here is the relevant code:

private void tabpage2_Leave(object sender, EventArgs e) 
{ 
    if (Validatetabpage2() == -1) 
    { 
        this.tabcontrol.SelectedTab =this.tabpage2; 
    } 
}
+3
2

TabControl . e.Cancel true tabcontrol .

private bool _cancelLeaving = false;

private void tabpage2_Leave(object sender, EventArgs e)
{
    _cancelLeaving = Validatetabpage2() == -1;
}

private void tabcontrol_Selecting(object sender, TabControlCancelEventArgs e)
{
    e.Cancel = _cancelLeaving;
    _cancelLeaving = false;
}
+1

, Validating .

. SelectedIndex , , CausesValidation = true. , Validating , - .

, , , .

Form Shown (Form_Load ), .

:

private void Form_Shown(object sender, System.EventArgs e)
{
     // Focus on the first tab page
     tabControl1.TabPages[0].Focus(); 
     tabControl1.TabPages[0].CausesValidation = true; 

     tabControl1.TabPages[0].Validating += new CancelEventHandler(Page1_Validating);
     tabControl1.TabPages[1].Validating += new CancelEventHandler(Page2_Validating);
 }

    void Page1_Validating(object sender, CancelEventArgs e)
    {
        if (textBox1.Text == "")
        {
            e.Cancel = true; 
        }
    }

    void Page2_Validating(object sender, CancelEventArgs e)
    {
        if (checkBox1.Checked   == false)
        {
            e.Cancel = true;
        }
    }

private void tabControl1_SelectedIndexChanged(object sender, System.EventArgs e) 
{ 
     // Whenever the current tab page changes
     tabControl1.TabPages[tabControl1.SelectedIndex].Focus(); 
     tabControl1.TabPages[tabControl1.SelectedIndex].CausesValidation = true; 
}
+1

Source: https://habr.com/ru/post/1729332/


All Articles