检查是否选择了特定选项卡页(活动)

发布于 2024-12-23 06:28:46 字数 99 浏览 1 评论 0原文

我正在创建一个事件来检查选项卡控件中的特定选项卡页是否处于活动状态。

重点是,如果选项卡控件中的选项卡页是当前选定的选项卡,则会触发事件。有什么代码可以满足我的需要吗?

I am making an event to check if specific tab page in a tab control is active.

The point is, it will trigger an event if that tab page in a tab control is the currently selected tab. Any code that will give me what I need?

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

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

发布评论

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

评论(6

赤濁 2024-12-30 06:28:46

假设您正在 Winform 中查看,该选项卡有一个 SelectedIndexChanged 事件,

现在您可以在其中检查特定选项卡并继续执行逻辑

private void tab1_SelectedIndexChanged(object sender, EventArgs e)
{
     if (tab1.SelectedTab == tab1.TabPages["tabname"])//your specific tabname
     {
         // your stuff
     }
}

Assuming you are looking out in Winform, there is a SelectedIndexChanged event for the tab

Now in it you could check for your specific tab and proceed with the logic

private void tab1_SelectedIndexChanged(object sender, EventArgs e)
{
     if (tab1.SelectedTab == tab1.TabPages["tabname"])//your specific tabname
     {
         // your stuff
     }
}
﹉夏雨初晴づ 2024-12-30 06:28:46

检查特定选项卡页是否是选项卡控件当前选定的页面很容易;只需使用选项卡控件的 SelectedTab 属性:

if (tabControl1.SelectedTab == someTabPage)
{
// Do stuff here...
}

如果代码是基于除所选选项卡页之外的某些事件执行的,则这会更有用(在这种情况下,SelectedIndexChanged 将是更好的选择)。

例如,我有一个应用程序,它使用计时器定期通过 TCP/IP 连接轮询内容,但为了避免不必要的 TCP/IP 流量,我只轮询更新当前选定选项卡页面中的 GUI 控件的内容。

To check if a specific tab page is the currently selected page of a tab control is easy; just use the SelectedTab property of the tab control:

if (tabControl1.SelectedTab == someTabPage)
{
// Do stuff here...
}

This is more useful if the code is executed based on some event other than the tab page being selected (in which case SelectedIndexChanged would be a better choice).

For example I have an application that uses a timer to regularly poll stuff over TCP/IP connection, but to avoid unnecessary TCP/IP traffic I only poll things that update GUI controls in the currently selected tab page.

打小就很酷 2024-12-30 06:28:46

在.Net 4中可以使用

if (tabControl1.Controls[5] == tabControl1.SelectedTab)
                MessageBox.Show("Tab 5 Is Selected");

OR

if ( tabpage5 == tabControl1.SelectedTab)
         MessageBox.Show("Tab 5 Is Selected");

in .Net 4 can use

if (tabControl1.Controls[5] == tabControl1.SelectedTab)
                MessageBox.Show("Tab 5 Is Selected");

OR

if ( tabpage5 == tabControl1.SelectedTab)
         MessageBox.Show("Tab 5 Is Selected");
泪是无色的血 2024-12-30 06:28:46

无论出于何种原因,上述内容对我不起作用。这就是所做的:

if (tabControl.SelectedTab.Name == "tabName" )
{
     .. do stuff
}

其中 tabControl.SelectedTab.Name 是分配给 tabcontrol 本身页面的名称属性。

For whatever reason the above would not work for me. This is what did:

if (tabControl.SelectedTab.Name == "tabName" )
{
     .. do stuff
}

where tabControl.SelectedTab.Name is the name attribute assigned to the page in the tabcontrol itself.

笑着哭最痛 2024-12-30 06:28:46

我认为使用事件 tabPage1.Enter 更方便。

tabPage1.Enter += new System.EventHandler(tabPage1_Enter);

private void tabPage1_Enter(object sender, EventArgs e)
{
    MessageBox.Show("you entered tabPage1");
}

当不同选项卡有不同的逻辑时,这比嵌套 if-else 语句更好。并且更适合将来添加新选项卡的情况。

请注意,如果加载表单并且默认打开 tabPage1,则会触发此事件。

I think that using the event tabPage1.Enter is more convenient.

tabPage1.Enter += new System.EventHandler(tabPage1_Enter);

private void tabPage1_Enter(object sender, EventArgs e)
{
    MessageBox.Show("you entered tabPage1");
}

This is better than having nested if-else statement when you have different logic for different tabs. And more suitable in case new tabs may be added in the future.

Note that this event fires if the form loads and tabPage1 is opened by default.

十雾 2024-12-30 06:28:46

这也可以。

if (tabControl.SelectedTab.Text == "tabText" )
{
    .. do stuff
}

This can work as well.

if (tabControl.SelectedTab.Text == "tabText" )
{
    .. do stuff
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文