TabControl.TabPageCollection.Contains(TabPage) Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Determines whether a specified tab page is in the collection.

C#
public bool Contains(System.Windows.Forms.TabPage page);

Parameters

page
TabPage

The TabPage to locate in the collection.

Returns

true if the specified TabPage is in the collection; otherwise, false.

Exceptions

The value of page is null.

Examples

The following code example initially creates a TabControl with three TabPage objects. The first two tab pages are added directly to tabControl1 by the AddRange method. The Contains method determines tabPage3 is part of the tabControl1 controls collection. Because tabPage3 was not found in this collection, it was added by the Add method.

C#
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    private TabControl tabControl1;
    private TabPage tabPage1;
    private TabPage tabPage2;
    private TabPage tabPage3;

    public Form1()
    {
        this.tabControl1 = new TabControl();
        string[] tabText = {"tabPage1", "tabPage2", "tabPage3"};
        this.tabPage1 = new TabPage(tabText[0]);
        this.tabPage2 = new TabPage(tabText[1]);
        this.tabPage3 = new TabPage(tabText[2]);

        // Populates the tabControl1 with two tab pages.
        this.tabControl1.TabPages.AddRange(new TabPage[] {
            tabPage1, tabPage2});

        // Checks the tabControl1 controls collection for tabPage3.
        // Adds tabPage3 to tabControl1 if it is not in the collection.
        if (!tabControl1.TabPages.Contains(tabPage3))
            this.tabControl1.TabPages.Add(tabPage3);

        this.tabControl1.Location = new Point(25, 25);
        this.tabControl1.Size = new Size(250, 250);

        this.ClientSize = new Size(300, 300);
        this.Controls.Add(tabControl1);
    }

    static void Main() 
    {
        Application.Run(new Form1());
    }
}

Applies to

Product Versions
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also