views:

1296

answers:

3

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.

+1  A: 

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);
    }
}
Hath
+1  A: 

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
Tim Robinson
I don't see an improvement on using a UserControl.
Simon
A: 

What's your objection to creating a user control and hosting it in the tab page? It seems like, semantically, this is what you are trying to do.

Hath's solution is actually quite nice...

-Scott

Scott P
This should be a comment not a separate answer.
mlimber