简体   繁体   English

标签控制离开

[英]Tabpage control leave

I have a tab control and 3 tabpages in it. 我有一个选项卡控件和3个tabpages。 ( C#) ( C#)

if i am in tab 2, and edit a text box value and then click tab 3, i need to validate what was enetered in the text box. 如果我在选项卡2中,并编辑文本框值,然后单击选项卡3,我需要验证文本框中的内容。 if correct i should allow to to switch to tab 3 else should remain in tab 2 it self how do i achieve this? 如果正确我应该允许切换到选项卡3否则应该保留在选项卡2它自己如何实现这一点?

iam curently handling the "leave" event of the tabpage2, i validate the text box value there and if found invalid i set as tabcontrol.Selectedtab = tabpage2; 我实际上处理tabpage2的“离开”事件,我验证那里的文本框值,如果发现无效,我设置为tabcontrol.Selectedtab = tabpage2; this does the validation but switches to new tab! 这会进行验证,但会切换到新标签! how could i restrict the navigation. 我怎么能限制导航。

I am a novice to C#, so may be i am handling a wrong event! 我是C#的新手,所以我可能正在处理错误的事件!

Here is the relevant code: 这是相关代码:

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

You can use the TabControl Selecting event to cancel switching pages. 您可以使用TabControl选择事件来取消切换页面。 Setting e.Cancel to true in the event stops the tabcontrol from selecting a different tab. 在事件中将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;
}

While the other approaches may work, the Validating event is designed specifically for this. 虽然其他方法可能有效,但Validating事件是专门为此而设计的。

Here's how it works. 这是它的工作原理。 When the SelectedIndex of the tab control changes, set the focus to the newly selected page and as well as CausesValidation = true. 当选项卡控件的SelectedIndex更改时,将焦点设置为新选择的页面,以及CausesValidation = true。 This ensures the Validating event will called if the user tries to leave the tab in any way. 这可确保在用户尝试以任何方式离开选项卡时将调用Validating事件。

Then do your normal validation in a page specific Validating event and cancel if required. 然后在页面特定的验证事件中进行常规验证,并在需要时取消。

You need to make sure to set the initial selected tab page in the Form Shown event (Form_Load will not work) and also wire up the tab page specific validating events. 您需要确保在Form Shown事件中设置初始选定的选项卡页面(Form_Load将不起作用),并连接标签页特定的验证事件。

Here's an example: 这是一个例子:

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; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM