Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a subclass of TabPage that I can edit like a UserControl?

I want to create a subclass of TabPage that contains some control, and I want to control the layout and properties of those controls through the designer. However, if I open my subclass in the designer, I can't position them like I could on a UserControl. I don't want to have to create a TabPage with an UserControl instance on it, I want to design the TabPage directly.

How do I do that? I've tried changing the Designer and DesignerCategory attributes, but I haven't found any values that help.

like image 293
Simon Avatar asked Dec 30 '22 08:12

Simon


2 Answers

I've had a similar problem in the past.

What i did first was switch from inheriting Usercontrol to tabpage like so

class UserInterface : UserControl // Do designer bit then change it to

class UserInterface : TabPage

Second i Just put all my controls and stuff in the usercontrol and docked that into a tabpage.

third i've made a generic class that takes any usercontrol and does the docking automatically.

so you can take your 'UserInterface' class and just get a type that you can add to a 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);
    }
}
like image 89
Hath Avatar answered Jan 17 '23 14:01

Hath


I handle this by designing the pages themselves as individual forms, which are then hosted inside tab pages at runtime.

How do you put a form inside a 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
like image 29
Tim Robinson Avatar answered Jan 17 '23 15:01

Tim Robinson