c# - 如何创建可以像 UserControl 一样进行编辑的 TabPage 子类?

标签 c# .net winforms custom-controls

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

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

最佳答案

我以前遇到过类似的问题。

我首先做的是像这样从继承 Usercontrol 切换到 tabpage

class UserInterface : UserControl//做设计师位然后改成

用户界面类: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);
    }
}

关于c# - 如何创建可以像 UserControl 一样进行编辑的 TabPage 子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/253517/

相关文章:

c# - 自定义控件 - 属性框中的可单击链接

wpf - 从 WinForms 迁移到 WPF

c# - 为什么我的 IAuthorizationPolicy 没有使用我的 CustomPrincipal 设置 Thread.CurrentPrincipal?

c# -\如果另一个实体与表同名,则无法将实体映射到表

后台 worker 中的.net数据读取器

c# - 性能受到影响?匿名类型列表来实现类似 List<int, string, ...> (多种类型的多维列表)在 c# 中)

c# - 引用中断控制台应用程序在其他机器上运行

C# 抽象类错误

c# - 从 HttpModule 调用时 Membership.GetUser() 失败

c# - 如何在 Windows 上运行的自托管 ServiceStack 中获取用户名