Edit

Share via


TabControl.TabPageCollection.Contains(TabPage) Method

Definition

Determines whether a specified tab page is in the collection.

public:
 bool Contains(System::Windows::Forms::TabPage ^ page);
public bool Contains (System.Windows.Forms.TabPage page);
member this.Contains : System.Windows.Forms.TabPage -> bool
Public Function Contains (page As TabPage) As Boolean

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.

using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class Form1: public Form
{
private:
   TabControl^ tabControl1;
   TabPage^ tabPage1;
   TabPage^ tabPage2;
   TabPage^ tabPage3;

public:
   Form1()
   {
      this->tabControl1 = gcnew TabControl;
      array<System::String^>^tabText = {"tabPage1","tabPage2","tabPage3"};
      this->tabPage1 = gcnew TabPage( tabText[ 0 ]->ToString() );
      this->tabPage2 = gcnew TabPage( tabText[ 1 ]->ToString() );
      this->tabPage3 = gcnew TabPage( tabText[ 2 ]->ToString() );
      
      // Populates the tabControl1 with two tab pages.
      array<TabPage^>^tabPages = {tabPage1,tabPage2};
      this->tabControl1->TabPages->AddRange( tabPages );
      
      // 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 = Point(25,25);
      this->tabControl1->Size = System::Drawing::Size( 250, 250 );
      this->ClientSize = System::Drawing::Size( 300, 300 );
      this->Controls->Add( tabControl1 );
   }

};

int main()
{
   Application::Run( gcnew Form1 );
}
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());
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Public Class Form1
    Inherits Form
    Private tabControl1 As TabControl
    Private tabPage1 As TabPage
    Private tabPage2 As TabPage
    Private tabPage3 As TabPage

    Public Sub New()
        Me.tabControl1 = New TabControl()
        Dim tabText As String() = {"tabPage1", "tabPage2", "tabPage3"}
        Me.tabPage1 = New TabPage(tabText(0))
        Me.tabPage2 = New TabPage(tabText(1))
        Me.tabPage3 = New TabPage(tabText(2))

        ' Populates the tabControl1 with two tab pages.      
        Me.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) = False Then
            Me.tabControl1.TabPages.Add(tabPage3)
        End If

        Me.tabControl1.Location = New Point(25, 25)
        Me.tabControl1.Size = New Size(250, 250)

        Me.ClientSize = New Size(300, 300)
        Me.Controls.Add(tabControl1)
    End Sub

    Shared Sub Main()
        Application.Run(New Form1())
    End Sub
End Class

Applies to

See also