Page 1 of 1

Dynamically Add TabPage To TabControl

#1 MATTtheSEAHAWK   User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Posted 06 April 2011 - 12:29 PM

Hello everybody! What this tutorial is going to be about is adding controls to IContainers from the programs code in Visual C#. First I am going to explain a few things before I start the tutorial.

You are probably asking what an IContainer is. An IContainer is an interface included in the .Net framework that allows controls to have controls inside of them when the main control implements the interface. A few examples would be group boxes, tab page, and panels.

Ok now we are going to start the tutorial. First lets open up Visual C# and create a new windows form application. You can name it whatever you want but I am going to name it DicTutorial. And here is what your screen should look like so far:

Posted Image

Ok after we have done that we have our form and we can add our IContainer control! For this tutorial I am going to use a Tab Control and we are going to add a tab page in the tab control button to it. So now what we are going to do is select a Tab Control from the toolbox and double click it to add it to the form. After that we are going to click on the tab control once and go to its properties. We are going to look for the dock property and set it to full for a nice and clean look. Our form should now look like this:

Posted Image

Now comes the time when we start to actually code the application. First what we are going to do is double click the top of the form so we can go into the Form1_Load event. Ok first lets create our new tab page which is going into the tab control. To do that we just have to add this code to the application which will create a tab page with the title of IContainer example. The code: TabPage page = new TabPage("IContainer Example");. Ok after we have done that we can add our button. Set its properties and click event. And then add it to the tab page via the code and make it the selected tab. Here is the code for that.

            TabPage page = new TabPage("IContainer Example"); //create our IContainer/TabPage
            Button button = new Button(); //create our button
            button.Text = "Hello Button!"; //set our buttons text
            button.Dock = DockStyle.Fill; //make our button fill up the tab page when added
            button.Click += new EventHandler(button_Click); //this adds the below button_Click event
            page.Controls.Add(button); //add our button to the tab page
            tabControl1.TabPages.Add(page); //add our tab page to the tab control
            tabControl1.SelectedTab = page; //make our tab the selected tab

Here is what the code we have done so far should look like right now:

Posted Image

If you look in that code you can notice that there is this code: throw new NomImplementedException(); in the button click event. Lets take that out now and replace it with MessageBox.Show("You added a control to an IContainer programmatically! Congrats!"); After that you are done and this is what the final code that we made should look like:

Posted Image

Now if you run the program you should get a form thats adjustable and the controls resize with it. Inside there should be a tab control and in that a tab page with a dock style filled button. Now if you click the button you will get a message telling you what you have just done ;) Here is what it all looks like:

Posted Image

Well that is the end of the tutorial folks and I hoped you learned something. But before I go I am going to say that the way I showed you to do this is not the only way. Another way you can do that is to make a re-usable class that implements the tab page class and then you can add controls on instantiation. Here is a link to a snippet that I posted as an example of that:


Hope you enjoyed the tutorial :)

Is This A Good Question/Topic? 2
  • +

Replies To: Dynamically Add TabPage To TabControl

#2 dana sorani   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-March 14

Posted 06 March 2014 - 08:53 AM

Good Tutorial , but try to resize image !
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1