Create a new copy of a tab

Discussion in 'Programming General' started by hampe-92, Dec 7, 2008.

Create a new copy of a tab
  1. Unread #1 - Dec 7, 2008 at 4:07 PM
  2. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Create a new copy of a tab

    OK, let's say I have a tab control (tabControl1) which contains one tab page (tabPage1) when I start the program. Inside tabPage1 I have some random stuff (look at the code)... when I click button1 i want to create a new exact copy of tabPage1.. so,

    ok, so here is what makes up tabControl1 with tabPage1:
    Code:
                // 
                // tabControl1
                // 
                this.tabControl1.Controls.Add(this.tabPage1);
                this.tabControl1.Location = new System.Drawing.Point(12, 12);
                this.tabControl1.Name = "tabControl1";
                this.tabControl1.SelectedIndex = 0;
                this.tabControl1.Size = new System.Drawing.Size(251, 183);
                this.tabControl1.TabIndex = 1;
                // 
                // tabPage1
                // 
                this.tabPage1.Controls.Add(this.radioButton1);
                this.tabPage1.Controls.Add(this.pictureBox1);
                this.tabPage1.Controls.Add(this.textBox1);
                this.tabPage1.Location = new System.Drawing.Point(4, 22);
                this.tabPage1.Name = "tabPage1";
                this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
                this.tabPage1.Size = new System.Drawing.Size(243, 157);
                this.tabPage1.TabIndex = 0;
                this.tabPage1.Text = "tabPage1";
                this.tabPage1.UseVisualStyleBackColor = true;
    
    as you see there is some random controls in there like radio button and picture box and so on...


    this code is the properties for those controls:
    Code:
                // 
                // textBox1
                // 
                this.textBox1.Location = new System.Drawing.Point(6, 6);
                this.textBox1.Name = "textBox1";
                this.textBox1.Size = new System.Drawing.Size(126, 20);
                this.textBox1.TabIndex = 0;
                // 
                // pictureBox1
                // 
                this.pictureBox1.BackColor = System.Drawing.Color.Black;
                this.pictureBox1.Location = new System.Drawing.Point(50, 64);
                this.pictureBox1.Name = "pictureBox1";
                this.pictureBox1.Size = new System.Drawing.Size(100, 50);
                this.pictureBox1.TabIndex = 2;
                this.pictureBox1.TabStop = false;
                // 
                // radioButton1
                // 
                this.radioButton1.AutoSize = true;
                this.radioButton1.Location = new System.Drawing.Point(65, 120);
                this.radioButton1.Name = "radioButton1";
                this.radioButton1.Size = new System.Drawing.Size(85, 17);
                this.radioButton1.TabIndex = 3;
                this.radioButton1.TabStop = true;
                this.radioButton1.Text = "radioButton1";
                this.radioButton1.UseVisualStyleBackColor = true;
    
    if I would like to create a new tab with the exact same content at the same position when i click a button (button1) I could do like this:

    Code:
    
            private void button2_Click(object sender, EventArgs e)
            {
                int x = tabControl1.TabCount + 1;
                TabPage tab = new TabPage("Tab " + x.ToString());
    
    
                RadioButton rad = new RadioButton();
                TextBox text = new TextBox();
                PictureBox pic = new PictureBox();
    
                //
                // textBox1
                // 
                text.Location = new System.Drawing.Point(6, 6);
                text.Size = new System.Drawing.Size(126, 20);
                text.TabIndex = 0;
                // 
                // pictureBox1
                // 
                pic.BackColor = System.Drawing.Color.Black;
                pic.Location = new System.Drawing.Point(50, 64);
                pic.Size = new System.Drawing.Size(100, 50);
                pic.TabIndex = 2;
                pic.TabStop = false;
                // 
                // radioButton1
                // 
                rad.AutoSize = true;
                rad.Location = new System.Drawing.Point(65, 120);
                rad.Size = new System.Drawing.Size(85, 17);
                rad.TabIndex = 3;
                rad.TabStop = true;
                rad.Text = "radioButton1";
                rad.UseVisualStyleBackColor = true;
    
    
    
                tab.Controls.Add(rad);
                tab.Controls.Add(text);
                tab.Controls.Add(pic);
    
    
                tabControl1.TabPages.Add(tab);
            }
    
    Now, finally to my point, there will be alot of code by doing this, and my question is; is there any easier way to do this? like copying tab1 in some way?

    Thx in advance :)
     
  3. Unread #2 - Dec 7, 2008 at 5:33 PM
  4. Swan
    Joined:
    Jan 23, 2007
    Posts:
    4,957
    Referrals:
    0
    Sythe Gold:
    0
    Sythe's 10th Anniversary Member of the Month Winner

    Swan When They Cry...
    Retired Global Moderator

    Create a new copy of a tab

    Well, this is where Object Oriented Programming is wonderful to use.

    In .Net, most of the things you use are classes. A button is essentially a class inheriting from the System.Windows.Forms.UserControl class (I think that is right). Tab pages are no exception. What you need to do is find out how to create a new tab page using the tab page class with exactly the same data as your first tab page, then find out how to add that to your tab control.

    MSDN is a great place to start.

    Here:
    http://msdn.microsoft.com/en-au/library/system.windows.forms.tabcontrol.aspx

    http://msdn.microsoft.com/en-au/library/system.windows.forms.tabpage.aspx
     
  5. Unread #3 - Dec 8, 2008 at 10:02 AM
  6. hampe-92
    Joined:
    Jul 10, 2008
    Posts:
    328
    Referrals:
    0
    Sythe Gold:
    0

    hampe-92 Forum Addict

    Create a new copy of a tab

    ok thx, I will take a look at that later, but I found out an other solution for my project so I don't need to use tabs anymore:), but it's always good to know ;)... so, I will take a look at that later
     
< Useful RuneScape Functions | Hopefully a simple problem.... >

Users viewing this thread
1 guest


 
 
Adblock breaks this site