开发者

How can i programmatically add a Tab to a form during runtime?

I googled this and still cant get it to work. I know how to add a tab using the toolbox. I have also read about how to do it programmatically, but i still dont get it. (MSVC# Express 2010)

I have an easy project set up. Just a windows Form with a TabControl in it, i used the Designer to add a new TabControl and made that TabControl public instead of private.

I wrote this code to a) access the Windows Form b) add a tabpage.

The code compiles just fine, but the Tabpage is not displayed during runtime.

static class Progra开发者_StackOverflowm
{
    [STAThread]
    static void Main()
    {
        Application.SetCompatibleTextRenderingDefault(false);
        Application.EnableVisualStyles();
        Form1 ApplicationMainForm = new Form1();
        Application.Run(ApplicationMainForm); //LABEL B

        ApplicationMainForm.tabControl1.TabPages.Add("MyPage"); //LABEL A
    }
}

How can i get the form to display my Tabpage? My TabPage is displayed when the Lines A and B change position. Am i missing an update method, oder is the TabPage Add never called until the application closes?

Edit #1: Some minor edits. Edit #2: Edited in some more examplecode. Edit #3: Removed some earlier / irelevant points. Edit #4: Found a hint and edited this information in


Form1.tabControl1.Controls.Add(myNewTabItem);

The tab control is a collection of tab pages, so you add tab pages like you add any control to a collection. Note that the tabs show up in the order you add them.


If you are trying to add a tab to the form at runtime, probably the issue is that you are trying to adjust the form definition instead of the specific instance of that form that you are currently displaying. When a form is opened, it is an instance of the form definition. You have to find that instance in order to modify its properties. Therefore, you would have to have the other part of your program somehow know about that particular instance of your form through something like a reference variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜