-3

I have two tabs that I know the names of (design and picture) and I want to remove every other tab in the tab control. (I want to remove all the tabpages excepts the ones I know the names of.)

I have searched the internet and I found:

string tabToRemove = "tabPageName";

for (int i = 0; i < tabControlMain.TabPages.Count; i++)
{
    if (tabControlMain.TabPages[i].Name.Equals(tabToRemove, StringComparison.OrdinalIgnoreCase))
    {
       tabControlMain.TabPages.RemoveAt(i);
       break;
    }
}

but this is not what I'm looking for as I do not know the strings of the tabs I want to remove. I only know the names of two.

4

1 回答 1

1

我在这里假设了很多关于您的问题,但我想您想删除所有标签页,除了您知道名称的标签页。

如果是这种情况,那么您将使用已知的标签页名称(准确拼写)填写一个列表。
然后开始向后循环以删除不符合您要求的标签页。

List<string> pagesToKeep = new List<string>() {"Design", "Picture"};
for (int i = tabControlMain.TabPages.Count - 1; i>=0; i--)
{
    string curName = tabControlMain.TabPages[i].Name;
    if(!pagesToKeep.Contains(curName))
    {
        tabControlMain.TabPages.RemoveAt(i);
    }
}

这里需要后向循环,因为当您从集合中删除一个项目时,元素的总数会发生变化,并且您不能安全地使用 end for 条件。

于 2013-07-21T09:31:49.350 回答