为什么标签页正文没有使用 .NET 标签控件更新? [英] Why is the tab page body not updating with a .NET tab control?

查看:28
本文介绍了为什么标签页正文没有使用 .NET 标签控件更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 C# (Visual Studio 2010) 中的 .NET TabControl 时遇到了一个奇怪的问题.启动 Windows 窗体应用程序.添加选项卡控件和按钮.在两个标签页上添加两个不同的标签,以便您可以区分它们.该按钮的目的只是充当下一个按钮;使用以下代码订阅其 Click 事件:

I am having a strange problem with the .NET TabControl in C# (Visual Studio 2010). Start a Windows Forms Application. Add a tab control and a button. Add two different labels to the two tab pages so you can differentiate them. The purpose of the button is just to act as a next button; subscribe to the its Click event with the code:

tabControl1.SelectTab(1);

假设用户在第一个选项卡上输入错误,因此当他们尝试转到第二个选项卡时,我们希望将它们发送回去,因此请使用以下代码订阅选项卡控件的 SelectedIndexChanged 事件:

Let's assume the user entered something wrong on the first tab, so when they try to go to the second tab we want to send them back, so subscribe to the tab control's SelectedIndexChanged event with the code:

if(tabControl1.SelectedIndex == 1)
{
    tabControl1.SelectTab(0);
}

现在运行程序并单击按钮.您会注意到,根据顶部突出显示的选项卡判断,第一个选项卡页似乎被选中,正如您所期望的那样.但是,从实际出现在选项卡控件正文中的选项卡页来判断,显示的仍然是第二个选项卡页!调用各种控件的 Focus()、Update() 和 Refresh() 函数似乎没有帮助.这是怎么回事?

Now run the program and click the button. You will notice that as judged by the highlighted tab at the top, the first tab page is the one that appears to be selected, as you'd expect. However, as judged by the tab page that actually appears in the body of the tab control, it's still the second tab page that shows up! Calls to various controls' Focus(), Update(), and Refresh() functions don't seem to help. What is going on here?

推荐答案

我转载.这是事件处理程序的一个普遍问题,您可以通过像这样猛拉地垫来混淆本机 Windows 控件的填充.TreeView 是另一个非常容易出现这种问题的控件.

I repro. This is a generic problem with event handlers, you can confuse the stuffing out the native Windows control by jerking the floor mat like that. TreeView is another control that's very prone to this kind of trouble.

对于这样的问题,有一个优雅而通用的解决方案,您可以使用 Control.BeginInvoke() 来延迟命令.在本机控件完成事件生成和所有副作用完成后,它将稍后执行.这也解决了这个问题,就像这样:

There's an elegant and general solution for a problem like this, you can use Control.BeginInvoke() to delay the command. It will execute later after the native control is done with the event generation and all side-effects have been completed. Which solves this problem as well, like this:

    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e) {
        if (tabControl1.SelectedIndex == 1) {
            this.BeginInvoke(new Action(() => tabControl1.SelectTab(0)));
        }
    }

这篇关于为什么标签页正文没有使用 .NET 标签控件更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆