简体   繁体   English

检查tabControl c#中是否存在选项卡

[英]Checking if tab exists in tabControl c#

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... 您可以执行以下操作来检查是否存在具有相同名称的TabPage ...

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. 当前的操作是,创建一个带有名称的新TabPage,并检查该选项卡名称是否不具有相同名称,因为从字面上给了该名称,该名称将始终为true

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM