4

我想创建一个包含一些控件的 TabPage 的子类,并且我想通过设计器来控制这些控件的布局和属性。但是,如果我在设计器中打开我的子类,我不能像在 UserControl 上那样定位它们。我不想创建一个带有 UserControl 实例的 TabPage,我想直接设计 TabPage。

我怎么做?我尝试更改 Designer 和 DesignerCategory 属性,但没有找到任何有用的值。

4

2 回答 2

9

我过去也遇到过类似的问题。

我首先做的是像这样从继承用户控件切换到标签页

class UserInterface : UserControl // 做设计器位,然后将其更改为

类用户界面:标签页

其次,我只是将我所有的控件和东西放在用户控件中并将其停靠在一个标签页中。

第三,我创建了一个通用类,它接受任何用户控件并自动进行对接。

因此您可以使用“UserInterface”类并获得可以添加到 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);
    }
}
于 2008-10-31T16:58:23.087 回答
1

我通过将页面本身设计为单独的表单来处理这个问题,然后在运行时托管在标签页中。

你如何把一个表格放在一个里面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
于 2008-10-31T19:32:06.473 回答