如何正确地向另一个自定义控件添加一个自定义控件,使其以表格形式呈现?

迪迪

我已经在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();
        }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用XAML中的另一个自定义控件基类使WPF在视图中实例化一个自定义控件?

WPF:自定义控件属性已被另一个自定义控件错误注册

自定义控件从另一个函数选择器控件返回任务

Xpages-自定义控件从另一个自定义控件获取自定义属性

我可以创建一个使用Generic.xml中的另一个自定义控件的自定义控件吗

将属性绑定到自定义控件的另一个属性

是否可以在自定义View类中创建另一个控件?

从另一个自定义函数调用Google表格自定义函数

如何在自定义控件中创建一个按钮以触发onClick事件,并以自定义控件所在的主要形式对其进行处理?

如何在另一个自定义视图中添加自定义视图?

依赖于另一个表单控件的Angular 2自定义验证器

从自定义控件的另一个属性获取在设计器中设置的属性值

如何将自定义xtype添加到另一个视图?

从自定义视图类向UIViewController添加一个自定义视图

在同一个库中如何有许多wpf自定义控件?

自定义HTML元素扩展了另一个自定义元素

在另一个自定义 UIView 中嵌入自定义 UITableViewCell?

从另一个自定义重定向到自定义 html 页面

使用 Win32 API 向另一个应用程序添加自定义边框

将一个 Swing 自定义表单插入另一个自定义表单

如何从另一个“高级自定义字段”获取“高级自定义字段”选择值?

如何将自定义 ObservableCollection 元素复制到另一个自定义 ObservableCollection?

在Angularjs中添加另一个自定义插值器

Django tables2 为另一个模型添加自定义列

在自定义主题中从另一个子菜单添加子菜单

当自定义类型帖子与另一个自定义类型帖子有连接时,如何获取自定义类型帖子数据?

如何使用分段控件在一个表视图中维护两个自定义单元格?

如何从自定义Gson JsonSerializer调用另一个序列化器?

如何在自定义tableViewController中过渡到另一个viewController?