Hi,

I have an application with a main form, and a TabControl in the centre. When a user creates a new tab I want to have controls (Textboxes and labels etc) loaded onto the tab.

I was thinking of creating my own class that have these controls already defined in it.
I guess I will have to inherit the Tabpage class, have a custom initializing method to put my controls on-screen?

Then create a new object in the Main form:
CustomTabPage custTabPage = new CustomTabPage();
customTabPage.addControls();

tabControl1.Controls.Add(customTabPage);

I'm just guessing at the moment. Can someone guide me in the right direction?

Thanks

Recommended Answers

All 2 Replies

Hello johnnyturbo3,
You have got right direction. I have tried to recreate your task. Working very well.

class MyTabClass:TabPage
    {

        public MyTabClass()
        {
            TextBox textbox = new TextBox
            {
                Width = 100,
                Height = 25,
                Left = 25,
                Top = 25
            };

            this.Controls.Add(textbox);
        }

    }


/* call from form

     private void button2_Click(object sender, EventArgs e)
        {
            MyTabClass tab = new MyTabClass();
            this.tabControl1.TabPages.Add(tab);
        }

You can set any parameters for your class:
text, name....

Cheers

There is an annoyance with the class file that I've create for the custom Tabpage. Because I inherit the System.Windows.Forms.TabPage, Visual Studio thinks that my class is actually a Form, and thus has a Designer View and several warnings etc.

Is there anyway to inherit System.Windows.Forms.TabPage and not make the class think it's a Form?

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.