【问题标题】:Trying to create a custom panel尝试创建自定义面板
【发布时间】:2019-11-19 00:17:07
【问题描述】:

我正在尝试创建一个包含许多控件的自定义面板。这是我第一次创建这样的自定义控件。此示例包含一个面板,该面板内有一个按钮。

我不确定为什么单击按钮时面板没有添加到 Form1 中。
我的代码中是否缺少某些内容?

        private void button2_Click(object sender, EventArgs e)
        {
            streamingPanel sp = new streamingPanel();
            sp.Size = new Size(319, 240);
            Point point = new Point(50, 50);
            sp.Location = point;
            this.Controls.Add(sp);
        }
        public class streamingPanel : Panel
        {
            public System.Windows.Forms.Panel panel1;
            public System.Windows.Forms.Button button1;

            public streamingPanel()
            {
                InitializeComponent();
            }

            private void InitializeComponent()
            {
                this.panel1 = new System.Windows.Forms.Panel();
                this.button1 = new System.Windows.Forms.Button();
                this.panel1.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                this.panel1.Controls.Add(this.button1);
                this.panel1.Location = new System.Drawing.Point(483, 91);
                this.panel1.Name = "panel1";
                this.panel1.Size = new System.Drawing.Size(319, 240);
                this.panel1.TabIndex = 0;
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(100, 72);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(75, 23);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                this.button1.UseVisualStyleBackColor = true;
            }
        }

【问题讨论】:

  • 删除panel1你的自定义控件是Panel,然后做你为删除的panel1this所做的一切。并将按钮添加到this.Controls.Add(button1);
  • 是的,这就是解决方案。现在我明白了。这当然是一个小组。我只是盲目地复制了它在 Visual Studio 中的布局方式。感谢您的帮助!
  • 欢迎您,祝您好运。
  • 好的,谢谢!
  • 用于此的控件不是 Panel 而是 UserControl。

标签: c# custom-panel


【解决方案1】:

当您将控件添加到窗体时,观察 Visual Studio 编辑器的作用。代码在从表单的构造函数调用的方法InitializeComponent 中。例如,您忘记设置新控件的LocationSize

【讨论】:

  • 是的,我正在尝试遵循 Visual Studio 编辑器的功能。我在原始帖子中更改了代码。我在哪里添加了InitializeComponent 并设置了LocationSize,但我仍然看不到面板?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-19
  • 2022-01-16
  • 2017-03-11
  • 2021-11-26
  • 2018-04-11
  • 1970-01-01
相关资源
最近更新 更多