Michael Gerbracht
Michael Gerbracht

Reputation: 173

Copy controls in VS C# project

I would like to use a tab control in a C# VS project. The tabs should all contain a large number of controls. The tabs are generated dynamically. So what I would like to do is to design a panel containing all controls using the designer. Then, at runtime when a new tab is created I would like to copy the panel (or all of it's controls) to the new tab.

I guess that I have to create a new class that represents a panel. If a new tab is opened, I generate a new instance of the class and add this panel control to the new tab.

Them I have two questions: 1. Is my understanding correct or do you need to do it in another way? 2. How can I create a class that represents a panel with controls? If I do it in the designer I will create two windows forms, one containing the tab control and the other one containig the panel with the example controls. But this way the panel is part of the windows form and the class that is automatically generated also represents a windows form containing a panel with controls. So an instance of this class is a windows form, not a panel. So I can't add it to the tabs.

Can you help me with this problem (I am quite new to c# and oo-programming)?

P.S.: I guess you could also iterate through all controls of a panel and add them to another panel. DO you thing this would be easier? And how would you do it then?

Upvotes: 0

Views: 1059

Answers (2)

ssett
ssett

Reputation: 432

You're not going to create two windows forms, but one form containing the tab control, and the other an UserControl containing the panel.

public class MainForm : Forms
{

}

public class MyPanel: UserControl
{
}

You could drag&draw the UserControl in the design time. And in the runtime, initialize an instance of the UserControl, and then add it to the new tab.

newTab.Controls.Add(new MyPanel());

Upvotes: 1

pan4321
pan4321

Reputation: 891

You can certainly create a User Control and put all the controls in this single one.

If your different tabs have large part in common then you can simply go for the scenario by putting all common control plus extra controls in a single User Control and dynamically hiding and showing the inner controls of User Controls based on different tabs criteria. The benefit will be that you will be creating only one control which will work for different tabs based on conditions.

Otherwise just go for different User Controls for each tab based on criteria and attach them dynamically.

Hope this helps.

Upvotes: 0

Related Questions