I have a Form1 form with a File menu with items
New Tab
New
Open
Print
Exit
On the Form1, I have a tab with initially one tab displayed. The tab has 2 buttons.
When I click New Tab from the menu I want to duplicate the whole tab with the two buttons and open a new tab with focus to it.
How to do it.

Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 3, 2023, 8:29 AM EST
To add new tab with duplicate tab. Try below code:
private void newTabToolStripMenuItem_Click(object sender, EventArgs e) { // Create a new tab page with the same controls as the first tab TabPage newTabPage = new TabPage(); newTabPage.Text = "New Tab " + (tabControl.TabCount + 1); // Set a unique name foreach (Control control in tabPage1.Controls) { Control newControl = Activator.CreateInstance(control.GetType()); newControl.Location = control.Location; newControl.Size = control.Size; newControl.TabIndex = control.TabIndex; newControl.Visible = control.Visible; newTabPage.Controls.Add(newControl); } // Add the new tab to the tab control and select it tabControl.TabPages.Add(newTabPage); tabControl.SelectedTab = newTabPage; // Set focus to the first control on the new tab page newTabPage.SelectNextControl(new Control(), true, true, true, true); }Jayanth Devarayanadurga23 reputationPosted Mar 3, 2023, 8:45 AM EST
Can you provide the UserControl version code?
I am trying that as it is much simpler but when I add the new usercontrol.dll to toolbox it gives error.
I am creating that with C# Control Forms project.
Jayanth Devarayanadurga23 reputationPosted Mar 3, 2023, 8:50 AM EST
Okay. I am trying your code but this line gets red squggly.
Jayanth Devarayanadurga23 reputationPosted Mar 3, 2023, 8:58 AM EST
It's ot working. It creates tabs but blank tabs.
Jayanth Devarayanadurga23 reputationPosted Mar 3, 2023, 9:33 AM EST
Okay. I got it 99% working but there is a small issue.
After the 2nd tab gets created successfully then 3 and successive tabs remained blank and then I experimented and found that if after creating 2nd tab and then before creating 3 and successive tabs if tab1 is selected and then new tabs are created then it works file. Only the first tab has to be selected before creating new tabs and only then all the controls on tab1 appears on the successive tabs.private void button3_Click(object sender, EventArgs e) { // Create a new tab page with the same controls as the first tab TabPage newTabPage = new TabPage(); newTabPage.Text = "tabPage" + (tabControl1.TabCount + 1); // Set a unique name foreach (Control control in tabPage1.Controls) { Control newControl = (Control)Activator.CreateInstance(control.GetType()); newControl.Location = control.Location; newControl.Size = control.Size; newControl.TabIndex = control.TabIndex; newControl.Visible = control.Visible; newControl.Text = control.Text; newControl.BackColor = control.BackColor; newControl.ForeColor = control.ForeColor; newControl.Font = control.Font; newTabPage.Controls.Add(newControl); // Add the new tab to the tab control and select it } tabControl1.TabPages.Add(newTabPage); tabControl1.SelectedTab = newTabPage; // Set focus to the first control on the new tab page newTabPage.SelectNextControl(new Control(), true, true, true, true); }
Jayanth Devarayanadurga23 reputationPosted Mar 3, 2023, 9:40 AM EST
Issue solved. I fixed the code. Now it is working.
One more issue is there, the code doesn't work for TabControl on another Tab Control.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 3, 2023, 5:46 PM EST
Hi,
Try th following steps:
1. Create a new function to duplicate the tab and its contents:
private void DuplicateTab() { // Get the current tab and its contents TabPage currentTab = tabControl1.SelectedTab; Button btn1 = currentTab.Controls.Find("button1", true).FirstOrDefault() as Button; Button btn2 = currentTab.Controls.Find("button2", true).FirstOrDefault() as Button; // Create a new tab and add it to the tab control TabPage newTab = new TabPage(); newTab.Text = "New Tab"; tabControl1.TabPages.Add(newTab); // Copy the contents of the current tab to the new tab Button newBtn1 = new Button(); newBtn1.Text = btn1.Text; newBtn1.Location = btn1.Location; newBtn1.Size = btn1.Size; newTab.Controls.Add(newBtn1); Button newBtn2 = new Button(); newBtn2.Text = btn2.Text; newBtn2.Location = btn2.Location; newBtn2.Size = btn2.Size; newTab.Controls.Add(newBtn2); // Select the new tab tabControl1.SelectedTab = newTab; }This function retrieves the current tab and its contents, creates a new tab, copies the contents of the current tab to the new tab, and selects the new tab.
2. Add an event handler for the "New Tab" item:
private void newTabToolStripMenuItem_Click(object sender, EventArgs e) { DuplicateTab(); }This event handler calls the DuplicateTab() function when the "New Tab" item is clicked.
3. Add an event handler for the form's Load event to create the initial tab:
private void Form1_Load(object sender, EventArgs e) { TabPage initialTab = new TabPage(); initialTab.Text = "Initial Tab"; Button btn1 = new Button(); btn1.Text = "Button 1"; btn1.Location = new Point(10, 10); btn1.Size = new Size(75, 23); initialTab.Controls.Add(btn1); Button btn2 = new Button(); btn2.Text = "Button 2"; btn2.Location = new Point(100, 10); btn2.Size = new Size(75, 23); initialTab.Controls.Add(btn2); tabControl1.TabPages.Add(initialTab); }This event handler creates the initial tab and adds it to the tab control with two buttons.
With these steps, you should be able to duplicate the whole tab with the two buttons and open a new tab with focus to it when the "New Tab" item is clicked from the menu.