Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 993
MK5992 Avatar asked Mar 20 '26 02:03

MK5992


1 Answers

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);           
}
like image 153
Sriram Sakthivel Avatar answered Mar 21 '26 15:03

Sriram Sakthivel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!