How to create a subclass of TabPage that can be edited as a UserControl?

I want to create a subclass of TabPage that contains some control, and I want to control the layout and properties of these controls through the constructor. However, if I open my subclass in the designer, I cannot position them as I could on UserControl. I do not want to create a TabPage with an instance of UserControl, I want to directly create a TabPage.

How can I do it? I tried changing the Designer and DesignerCategory attributes, but I did not find any values ​​that help.

+4
source share
2 answers

I had a similar problem in the past.

What I did first is to switch from Usercontrol inheritance to tabs, for example:

class UserInterface: UserControl // Make a constructor bit, then change it to

class UserInterface: TabPage

Second i Just put all my controls and stuff in the usercontrol and pin it to the tab.

Third, I made a general class that takes any user control and does the docking automatically.

so that you can take your "UserInterface" class and just get the type you can add to System.Windows.Forms.TabControl

public class UserTabControl<T> : TabPage where T : UserControl, new () { private T _userControl; public T UserControl { get{ return _userControl;} set { _userControl = value; OnUserControlChanged(EventArgs.Empty); } } public event EventHandler UserControlChanged; protected virtual void OnUserControlChanged(EventArgs e) { //add user control docked to tabpage this.Controls.Clear(); UserControl.Dock = DockStyle.Fill; this.Controls.Add(UserControl); if (UserControlChanged != null) { UserControlChanged(this, e); } } public UserTabControl() : this("UserTabControl") { } public UserTabControl(string text) : this( new T(),text ) { } public UserTabControl(T userControl) : this(userControl, userControl.Name) { } public UserTabControl(T userControl, string tabtext) : base(tabtext) { InitializeComponent(); UserControl = userControl; } private void InitializeComponent() { this.SuspendLayout(); // // UserTabControl // this.BackColor = System.Drawing.Color.Transparent; this.Padding = new System.Windows.Forms.Padding(3); this.UseVisualStyleBackColor = true; this.ResumeLayout(false); } } 
+9
source

I handle this by creating the pages themselves as separate forms, which are then placed inside the tab pages at runtime.

How do you put a form inside TabPage ?

 form.TopLevel = false; form.Parent = tabPage; form.FormBorderStyle = FormBorderStyle.None; // otherwise you get a form with a // title bar inside the tab page, // which is a little odd 
+1
source

All Articles