I am creating tabs dynamically and I have managed to do it but when I try and make it check if the tab already exists so it cant add another one if just keeps adding them.
private void fourBtn_Click(object sender, EventArgs e)
{
string theName = "Level4";
TabPage tp = new TabPage(theName);
if (!tp.Name.Equals(theName)) {
tp.Name = theName;
tabControl1.TabPages.Add(tp);
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Multiline = true;
tp.Controls.Add(tb);
}
}
You can do the following to check if a TabPage exists with the same name...
tabControl1.TabPages.Contains(tabPage)
What you currently do is, you create a new TabPage with a name, and check if that tab name does not have the same name which was and always will evaluate to true
since you've literally given it that name.
private void fourBtn_Click(object sender, EventArgs e) {
string theName = "Level4";
TabPage tp = new TabPage(theName);
if (!tabControl1.TabPages.Contains(tabPage2)) {
tp.Name = theName;
tabControl1.TabPages.Add(tp);
TextBox tb = new TextBox();
tb.Dock = DockStyle.Fill;
tb.Multiline = true;
tp.Controls.Add(tb);
}
}
If you wish, you can even create a loop:
foreach(TabPage tab in tabControl1.TabPages){
if(tab.Name == tabName)
return true;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.