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 MakwanaPosted Mar 3, 2023, 1:29 PM
To add new tab with duplicate tab. Try below code:
Tuhin PaulPosted Mar 3, 2023, 10:46 PM
Hi,
Try th following steps:
1. Create a new function to duplicate the tab and its contents:
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:
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:
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.
Jayanth DevarayanadurgaPosted Mar 3, 2023, 2:40 PM
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.
Jayanth DevarayanadurgaPosted Mar 3, 2023, 2:33 PM
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 DevarayanadurgaPosted Mar 3, 2023, 1:58 PM
It's ot working. It creates tabs but blank tabs.
Jayanth DevarayanadurgaPosted Mar 3, 2023, 1:50 PM
Okay. I am trying your code but this line gets red squggly.
Jayanth DevarayanadurgaPosted Mar 3, 2023, 1:45 PM
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.