繁体   English   中英

如何将自定义控件正确添加到另一个自定义控件,以便它将以表单形式呈现?

[英]How do I properly add a custom control to another custom control so that it will render in a form?

我在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的WellControls。 我做错了什么?

您需要将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();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM