Reputation: 1297
I have lot of tab pages in my program and I want to add or remove these pages. I'm using the following statement to do it.
if(this.myControlForm.TabPages.Contains(tabPage1))
{
this.myControlForm.TabPages.Remove(tabPage1));
}
if(this.myControlForm.TabPages.Contains(tabPage2))
{
this.myControlForm.TabPages.Remove(tabPage2));
}
[...] //similar code for other tabPages
but it makes too much code when I have a lot of tabpages to add or remove so I want to add them in an array and make a foreach loop but I got an error
my code is :
object[] listToRemove = {tabPage1, tabPage2,tabPage3, ... tabPage20};
foreach ( object itr in listToRemove)
{
if(this.myControlForm.TabPages.Contains(itr))
{
this.myControlForm.TabPages.Remove(itr));
}
}
Why is this happening, can anyone please help?
Upvotes: 0
Views: 64
Reputation: 26846
Change your listToRemove
to be explicitly typed, i.e. from array of objects to the array of TabPage
.
Same story with itr
variable in loop.
Contains
and Remove
methods expects argument to be of type TabPage
, not object
- that's why you're getting errors.
TabPage[] listToRemove = {tabPage1, tabPage2,tabPage3, ... tabPage20};
foreach (TabPage itr in listToRemove)
{
if(this.myControlForm.TabPages.Contains(itr))
this.myControlForm.TabPages.Remove(itr);
}
Upvotes: 4