MK5992
MK5992

Reputation: 23

Adding UserControl to Tabpage

I have a user control that is added to tab page. Problem is it works fine the first time a new tab is added but when i click button to add new tab page again, the user controls doesnt appear on any tab including the first tab also...

public void addnewtab()
    {
        UserControl1 myUserControl;
        myUserControl = new UserControl1();
        myUserControl.Dock = DockStyle.Fill;
        myTabPage.Controls.Add(myUserControl);
        tabControl1.TabPages.Add(myTabPage);           
    }

this is my code. Please help.

Upvotes: 2

Views: 19769

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73442

It is not clear where you get myTabPage. You need to create new tabpage inside the method and add usercontrol into it.

public void AddNewTab()
{
    UserControl1 myUserControl = new UserControl1();
    myUserControl.Dock = DockStyle.Fill;
    TabPage myTabPage = new TabPage();//Create new tabpage
    myTabPage.Controls.Add(myUserControl);
    tabControl1.TabPages.Add(myTabPage);           
}

Upvotes: 9

Related Questions