【问题标题】:C# user control as a custom panelC# 用户控件作为自定义面板
【发布时间】:2009-08-31 09:39:22
【问题描述】:

我创建了自己的用户控件,它只包含一个面板:

当我在设计器中拖动一个 myPanel 对象,然后尝试在其上添加一个按钮时,该按钮实际上已添加到表单的控件中。

是否有我必须设置的属性/属性才能执行此操作,另一种方式来做我想做的事?

public class MyPanel : UserControl
{
    private Panel panelMain;

    public MyPanel()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.panelMain = new System.Windows.Forms.Panel();
        this.SuspendLayout();
        // 
        // panelMain
        // 
        this.panelMain.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.panelMain.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panelMain.Location = new System.Drawing.Point(0, 0);
        this.panelMain.Name = "panelMain";
        this.panelMain.Size = new System.Drawing.Size(150, 150);
        this.panelMain.TabIndex = 0;
        // 
        // myPanel
        // 
        this.Controls.Add(this.panelMain);
        this.Name = "MyPanel";
        this.ResumeLayout(false);
    }
}

【问题讨论】:

    标签: c# winforms user-controls


    【解决方案1】:

    看这里

    How to make a UserControl object acts as a control container design-time by using Visual C#

    但如果您只需要扩展 Panel 功能,最好直接从 Panel 继承。

    【讨论】:

    • 这就是我想要的。关于继承,我只是让问题变得更简单,当然我需要更复杂的东西。
    【解决方案2】:

    更准确地说,我执行以下操作:

    [Designer(typeof(myControlDesigner))] 
    public class MyPanel : UserControl
    {
        private TableLayoutPanel tableLayoutPanel1;
        private Panel panelMain;
        ...
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public Panel InnerPanel
        {
            get { return panelMain; }
            set { panelMain = value; }
        }
    
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public TableLayoutPanel InnerTableLayout
        {
            get { return tableLayoutPanel1; }
            set { tableLayoutPanel1 = value; }
        }
    }
    

    用 myControlDesigner 为 [Designer(typeof(myControlDesigner))] 为

    class myControlDesigner : ParentControlDesigner
    {
    
        public override void Initialize(IComponent component)
        {
    
            base.Initialize(component);
    
            MyPanel myPanel = component as MyPanel;
            this.EnableDesignMode(myPanel.InnerPanel, "InnerPanel");
            this.EnableDesignMode(myPanel.InnerTableLayout, "InnerTableLayout");
    
        }
    
    }
    

    (其他来源:Add Control to UserControl)

    【讨论】:

    • 我已使用您的方法来创建自定义面板。但是,当我在另一个用户控件中使用此自定义面板时,当我在设计器中拖动面板上的控件时,它们最初会出现。但是,当我重新编译时,它们消失了。它们仍然存在,但似乎自定义面板在它们之上/之后绘制。你有过这样的问题吗?我正在使用 VS 2005/.NET 2.0。
    • 没有,我不记得有这种问题,你有小样本吗?
    • @bsh152s 这是一个非常古老的答案,但我想补充一下“控件丢失”的问题:公开的 Panel/TableLayout 必须公开(公共修饰符)。在我使用此代码时,我还添加了 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 以确保 Visual Studio 序列化更改。
    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多