TabPage keeps being created even the tab page already exists in my tab control.
Please consider my code below:
void button1_Click(object sender, EventArgs e)
{
TabPage tabPage = new TabPage();
tabPage.Name = "TestNewTab";
tabPage.Text = "Tab Page";
// Check if the tabpage is not yet existing
if (!tabControl1.TabPages.Contains(tabPage))
{
// Add the new tab page
tabControl1.TabPages.Add(tabPage);
}
}
What’s wrong with my code?
Thanks.
My guess would be that TabPages.Contains is checking for an object reference, since you’re instantiating a new TabPage every time, it won’t be the same object. Try looping through the tab pages and comparing the Name property instead.