C# 用户控件作为自定义面板 [英] C# user control as a custom panel

查看:45
本文介绍了C# 用户控件作为自定义面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I create my own user control which only contains a panel :

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

When I drag a myPanel object in the designer and then try to add a button on it, the button is in fact added to the form's controls.

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

Is there a property/attribute I must set in order to perform this, an other way to do what I want ?

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

推荐答案

看这里

如何使用 Visual 使 UserControl 对象在设计时充当控件容器C#

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

But if you only need extended Panel functionality its better to inherit directly from Panel.

这篇关于C# 用户控件作为自定义面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆