如何创建TabPage的子类,以便像UserControl一样编辑?

问题描述:

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

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

我以前有过类似的问题。

我做了第一次是从继承用户控件到标签页,像这样

类的UserInterface开关:用户控件//不要设计师位,然后将其更改为

类的UserInterface:TabPage的

其次我只是把我的所有控件和用户控件中的东西,并将它们停靠在一个tabpage中。

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

,所以你可以把你“的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); 
    } 
} 
+0

为我工作!羞耻没有人接受这个恶毒。 – dcg 2011-10-17 09:56:06

+0

真正重要的部分是从TabPage更改为UserControl。出于某种原因,如果Visual Studio从TabPage继承,它将不会在设计器中显示该控件。更改为UserControl可立即解决此问题。现在它也出现在工具箱中。 – lanoxx 2011-11-15 01:20:17

我处理这种通过设计自己的网页作为单独的形式,然后在运行时将其托管在标签页内。

如何在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