如何创建的TabPage的子类,我可以编辑像一个用户控件?子类、控件、编辑、用户

2023-09-04 12:50:05 作者:差一点就长成帅哥

我想创建的TabPage的子类,包含了一些控制,我想控制通过设计布局和这些控件的属性。不过,如果我在设计器中打开我的子类,我不能像我可以在用户控件放置它们。我不希望有创建一个TabPage的,上面有一个用户控件实例,我想直接设计TabPage的。

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.

我该怎么办呢?我试着改变了设计和DesignerCategory属性,但我没有发现任何帮助值。

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

推荐答案

我已经在过去类似的问题。

I've had a similar problem in the past.

我所做的第一项是继承用户控件到标签页,像这样开关

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

类的UserInterface:用户控件//做设计一下,然后将其更改为

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

类的UserInterface:TabPage的

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.

这样你就可以把你的的UserInterface级和刚刚获得一个类型,你可以添加到System.Windows.Forms.TabControl

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);
    }
}