Michael
Michael

Reputation: 13636

How to delete tabPages?

I have TabControl that contain n tabPages(WinForm poject).

At some point I want to delete tabPages with specific name.

How do I implement it with optimal running time complexity?

Upvotes: 1

Views: 203

Answers (1)

BrunoLM
BrunoLM

Reputation: 100381

You can use System.Linq to find the tabPage with the desired name. If it exists you can remove it.

var tabPage = tabControl1.TabPages.OfType<TabPage>()
    .FirstOrDefault(o => o.Name == "SpecificName");

if (tabPage != null)
{
    tabControl1.TabPages.Remove(tabPage);
}

You can create an extension method to make it simpler

public static class TabControlExtender
{
    public static void Remove(this TabControl t, string name)
    {
        var tabPage = t.TabPages.OfType<TabPage>()
            .FirstOrDefault(o => o.Name == name);

        if (tabPage != null)
        {
            t.TabPages.Remove(tabPage);
        }
    }
}

Usage:

tabControl1.Remove("SpecificName");

Upvotes: 2

Related Questions