简体   繁体   English

从TabControl中删除选项,其中索引不是

[英]Remove Tabs from TabControl Where Index is Not

I am trying to remove all tabs from a TabControl where the tab index is not a specified index. 我试图从TabControl中删除选项卡索引不是指定索引的所有选项卡。

I have this so far: 到目前为止我有这个:

for (int i = tabcontrolOptions.TabCount - 1; i >= 0; i--)
{
    if (i != tabNo)
    {
        tabcontrolOptions.TabPages.RemoveAt(i);
    }
}

But that removes all tabs, and not the specified tabNo. 但是这会删除所有选项卡,而不是指定的tabNo。 What am I doing wrong? 我究竟做错了什么?

When you remove a tab, the tabs get reindexed . 删除选项卡后,选项卡将重新编制索引 Actually, what you're doing shouldn't be affected by that. 实际上,你所做的不应该受到影响。 But try the below anyway, maybe it will help. 但是无论如何都要尝试以下,也许它会有所帮助。

Refer to the tab object explicitly, not by index: 请明确引用选项卡对象,而不是索引:

var tabToKeep = tabcontrolOption.TabPages[tabNo];
for (int i = tabcontrolOptions.TabCount - 1; i >= 0; i--)
{
    if (tabcontrolOption.TabPages[i] != tabToKeep)
    {
        tabcontrolOptions.TabPages.RemoveAt(i);
    }
}

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

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