emiraslan emiraslan - 3 months ago 13
C# Question

Controls.Add(tabPage) vs. TabPages.Add(tabPage)

To create a new tab in winform application I found this in msdn:

string title = "TabPage " + (tabControl1.TabCount + 1).ToString();
TabPage tabPage1 = new TabPage(title);
tabControl1.TabPages.Add(tabPage1);


But when I add a tab manually from Form1.cs[Design] Visual Studio automatically creates this in Form1.Designer.cs for a new tab:

private System.Windows.Forms.TabPage tabPage1;
this.tabPage1 = new System.Windows.Forms.TabPage();
this.tabControl1.Controls.Add(this.tabPage1);


So, what is the main difference between
tabControl1.Controls.Add(tabPage1)
and
tabControl1.TabPages.Add(tabPage1);
?

Answer

There is no difference. Here is the source code for TabPageCollection.Add method:

public void Add(TabPage value) {
    if (value == null) {
        throw new ArgumentNullException("value");
    }
    owner.Controls.Add(value);
}

In above code, the owner is the TabControl which owns the TabPageCollection.

The benefit of using TabPageCollection is in 5 different overloads which it provides for adding tab pages to the TabControl.