Tab页控制离开

发布于 2024-08-18 18:51:39 字数 498 浏览 11 评论 0原文

我有一个选项卡控件和 3 个选项卡页。 ( C#)

如果我在选项卡 2 中,并编辑文本框值 然后单击选项卡 3,我需要验证文本框中输入的内容。 如果正确,我应该允许切换到选项卡 3,否则应该保留在选项卡 2 中 我该如何实现这一目标?

iam 目前正在处理 tabpage2 的“leave”事件, 我验证那里的文本框值,如果发现无效 我设置为 tabcontrol.Selectedtab = tabpage2;这确实 验证但切换到新选项卡!我怎样才能限制导航。

我是 C# 新手,所以可能我正在处理错误的事件!

这是相关代码:

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

I have a tab control and 3 tabpages in it. ( 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.
if correct i should allow to to switch to tab 3 else should remain in tab 2 it self
how do i achieve this?

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

Here is the relevant code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人海汹涌 2024-08-25 18:51:39

虽然其他方法可能有效,但验证事件是专门为此设计的。

这是它的工作原理。当选项卡控件的 SelectedIndex 更改时,将焦点设置到新选择的页面,并设置 CausesValidation = true。这确保了如果用户尝试以任何方式离开选项卡,将调用 Validating 事件。

然后在页面特定的验证事件中进行正常验证,并根据需要取消。

您需要确保在“显示表单”事件中设置初始选定的选项卡页(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; 
}

While the other approaches may work, the Validating event is designed specifically for this.

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. This ensures the Validating event will called if the user tries to leave the tab in any way.

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.

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; 
}
懵少女 2024-08-25 18:51:39

您可以使用 TabControl Selecting 事件来取消切换页面。在事件中将 e.Cancel 设置为 true 会阻止选项卡控件选择其他选项卡。

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

You can use the TabControl Selecting event to cancel switching pages. Setting e.Cancel to true in the event stops the tabcontrol from selecting a different tab.

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文