简体   繁体   English

检查TabControl中是否存在tab c#

[英]Check if tab exists in TabControl c#

I want to dynamically change the Tabs in a Tab Control depending on what options the user selects.我想根据用户选择的选项动态更改选项卡控件中的选项卡。

I have followed the example here TabControl.TabPageCollection.Contains Method (TabPage)我在这里按照示例TabControl.TabPageCollection.Contains Method (TabPage)

My code is as follows;我的代码如下;

private TabPage VulnTab;

VulnTab = new TabPage("Vulnerability");
if (tabControl1.TabPages.Contains(VulnTab) == true)
{
    tabControl1.SelectedTab = VulnTab;
}
else
{
    tabControl1.TabPages.Add(VulnTab);

    tabControl1.SelectedTab = VulnTab;
    var vuln = new vulnerability();
    tabControl1.SelectedTab.Controls.Add(vuln);
}

This is fired on a button click.这是在单击按钮时触发的。

On the first run, there is no VulnTab so it creates one successfully.第一次运行时,没有 VulnTab,因此它成功创建了一个。 However, on re-clicking the button it creates a new one again, and so on.但是,在重新单击该按钮时,它会再次创建一个新按钮,依此类推。

I want it to note on the 2nd button click that the tab page already exists and just go there.我想让它注意到在第二个按钮上点击标签页已经存在并且只有 go 在那里。

How can I achieve this?我怎样才能做到这一点?

Your problem is, that you are creating completely new page every time. 您的问题是,您每次都在创建一个全新的页面。 As you are defining private TabPage object, you could move it to the class (Form?) scope and check if it's null, like in example below: 在定义私有TabPage对象时,可以将其移至类(Form?)范围,并检查其是否为null,如下例所示:

private TabPage VulnTab;

private void ButtonClicked(object sender, EventArgs args)
{
    if (VulnTab != null)
    {
        tabControl1.SelectedTab = VulnTab;
    }
    else
    {
        VulnTab = new TabPage("Vulnerability");
        tabControl1.TabPages.Add(VulnTab);

        tabControl1.SelectedTab = VulnTab;
        var vuln = new vulnerability();
        tabControl1.SelectedTab.Controls.Add(vuln);
    }
}

Of course ButtonClicked is just whatever method fires your action of creating and/or changing the tab. 当然, ButtonClicked就是触发您创建和/或更改选项卡的任何方法。

For your future reference: If you create new object (by using keyword new ) you are creating completely new object. 供将来参考:如果创建新对象(通过使用关键字new ),则将创建全新的对象。 They might be identical, but they are not the same thing, like two apples which look the same aren't the same apple. 它们可能是相同的,但它们不是一回事,就像看起来相同的两个苹果不是同一个苹果。 Only under some specific conditions (object being simple type (numbers, DateTime) or having IEquatable implemented) two objects created in two separate places might be equal to .NET. 仅在某些特定条件下(对象是简单类型(数字,DateTime)或已实现IEquatable),在两个不同位置创建的两个对象才可能等于.NET。

VulnTab = new TabPage("Vulnerability");
if (tabControl1.TabPages.Contains(VulnTab) == true)

You just created a brand new TabPage . 您刚刚创建了一个全新的TabPage Obviously, that isn't in TabPages . 显然,这不在TabPages

You need to check the existing instance, by only creating it once. 您只需要创建一次就可以检查现有实例。

This is a better way:这是一个更好的方法:

if (tabControl1.TabPages.Contains(VulnTab) == true)
{
    tabControl1.SelectedTab = VulnTab;
}
else
{
    VulnTab = new TabPage("Vulnerability");
    tabControl1.TabPages.Add(VulnTab);

    tabControl1.SelectedTab = VulnTab;
    var vuln = new vulnerability();
    tabControl1.SelectedTab.Controls.Add(vuln);
}

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

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