【问题标题】:How do I properly add a custom control to another custom control so that it will render in a form?如何正确地将自定义控件添加到另一个自定义控件,以便它将呈现在表单中?
【发布时间】:2014-02-06 08:38:10
【问题描述】:

我在 C# 中创建了一个自定义控件。它的高度简化版本如下所示:

class WellControl : Control
    {                
        Pen circlePen = new Pen(Color.Black, 5);
        Brush wellShader = new SolidBrush(Color.BlueViolet);
        Brush wellBlanker = new SolidBrush(Color.LightGray);

        public WellControl(int wellNum)
        {
            InitializeComponent();
        }

        private void DrawWell()
        {
            using (Graphics well = this.CreateGraphics())
            {
                this.Size = new Size(WellSize, WellSize);
                if (this.selected)
                {
                    well.FillEllipse(wellShader, ellipseCoords);
                }
                else
                {
                    well.FillEllipse(wellBlanker, ellipseCoords);
                }

                well.DrawEllipse(circlePen, ellipseCoords);
                using (Font wellNumberFont = new Font("Arial", 14, FontStyle.Bold))
                {
                    well.DrawString(WellNum.ToString(), wellNumberFont, Brushes.Black, new Point(13, 13));
                }
            }
        }

        private void WellPaintEventHandler(object sender, EventArgs e)
        {
            DrawWell();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.Paint += new System.Windows.Forms.PaintEventHandler(WellPaintEventHandler);
            this.ResumeLayout();
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            if (disposing)
            {
                //dispose all the custom stuff
            }
        }
    }

当我将它添加到表单时,它会正确呈现(再次,简化示例):

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 4; i++)
            {
                WellControl newWell = new WellControl(i + 1);             
                newWell.Location = new Point(15, 50 * i);
                newWell.Size = new System.Drawing.Size(45, 45);
                this.Controls.Add(newWell);
            }
        }
     }

我有另一个自定义控件“Plate”,用于容纳许多“Wells”。旨在将孔均匀地绘制在板上的代码现在可能很糟糕,但我只是想看看 一些东西

class PlateControl : Control
    {

        Pen blackPen = new Pen(Color.Black, 3);

        public PlateControl()
        {
            this.Size = new Size(600, 800);
            List<WellControl> plateWells = new List<WellControl>();
            int column = 1;
            int row = 0;
            for (int i = 1; i <= 96; i++)
            {
                column = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(i / 8)));
                row = i % 8;
                WellControl newWell = new WellControl(i + 1);
                newWell.Name = "wellControl" + i;
                newWell.Location = new Point(column * 50, row * 50);
                newWell.Size = new System.Drawing.Size(45, 45);
                newWell.TabIndex = i;
                newWell.WellSize = 45;
                plateWells.Add(newWell);
                newWell.Visible = true;
            }
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.Paint += new System.Windows.Forms.PaintEventHandler(PlatePaintEventHandler);
            this.ResumeLayout();
        }

        private void DrawPlate()
        {
            using (Graphics plate = this.CreateGraphics())
            {
                Point topLeft = new Point(0, 0);
                Point topRight = new Point(600, 0);
                Point bottomRight = new Point(600, 400);
                Point bottomLeft = new Point(0, 400);

                plate.DrawLine(blackPen, topLeft, topRight);
                plate.DrawLine(blackPen, topRight, bottomRight);
                plate.DrawLine(blackPen, bottomRight, bottomLeft);
                plate.DrawLine(blackPen, bottomLeft, topLeft);
            }
        }

        private void PlatePaintEventHandler(object sender, EventArgs e)
        {
            DrawPlate();
        }
    }

如果我使用 this.Controls.Add(new PlateControl()) 将此控件添加到 Winform,则渲染矩形,但不会渲染我在构造函数循环中添加到 PlateControl 的 WellControl。我做错了什么?

【问题讨论】:

  • 你已经向我们展示了你的简化代码,但我认为你已经简化了很多,它不会编译你缺少一些变量的定义。
  • WellControl 有编译错误。

标签: c# winforms user-controls


【解决方案1】:

您需要将 WallControl 列表添加到板控件中。

public PlateControl()
        {
            this.Size = new Size(600, 800);
            List<WellControl> plateWells = new List<WellControl>();
            int column = 1;
            int row = 0;
            for (int i = 1; i <= 96; i++)
            {
                column = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(i / 8)));
                row = i % 8;
                WellControl newWell = new WellControl(i + 1);
                newWell.Name = "wellControl" + i;
                newWell.Location = new Point(column * 50, row * 50);
                newWell.Size = new System.Drawing.Size(45, 45);
                newWell.TabIndex = i;
                newWell.WellSize = 45;
                plateWells.Add(newWell);
                newWell.Visible = true;
            }
            this.Controls.AddRange(plateWells.ToArray());
            InitializeComponent();
        }

【讨论】:

  • Doh,你也打败了我,阻止我打字慢。
  • 做到了。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多