Tim van Gool
Tim van Gool

Reputation: 305

Removing dynamic tab pages from tab control

I have a TabControl called tc_Dashboard what I want to do is add dynamic tabs to that TabControl and remove them dynamically as well.

This is what I use to make the first dynamic tab.

tabTitle = "+";
TabPage tab = new TabPage(tabTitle);
tc_Dashboard.Controls.Add(tab);

when I try to remove it using the following code its give me an ArgumentNullException unhandled error.

if(tc_Dashboard.SelectedTab.Text == "+")
{
    tc_Dashboard.TabPages.Remove(tc_Dashboard.TabPages["+"]);
}

I've tried searching online for a solution but without success Any help would be appreciated

Upvotes: 0

Views: 1257

Answers (1)

ASh
ASh

Reputation: 35720

the simple solution is to remove SelectedTab

if(tc_Dashboard.SelectedTab.Text == "+")
     tc_Dashboard.TabPages.Remove(tc_Dashboard.SelectedTab)

why original code doesn't work?

if(tc_Dashboard.SelectedTab.Text == "+")
{
    tc_Dashboard.TabPages.Remove(tc_Dashboard.TabPages["+"]);
}

tc_Dashboard.TabPages["+"] is null, because there is no page with Name equal to +. This code

tabTitle = "+";
TabPage tab = new TabPage(tabTitle);
tc_Dashboard.Controls.Add(tab);

creates new tab and set Text property, but Name is empty

if you set Name for created tab,

 TabPage tab = new TabPage(tabTitle) { Name = "+" };

tc_Dashboard.TabPages["+"] will return tabPage, not null

Upvotes: 1

Related Questions