C#批量动态生成控件

有时候我们需要动态添加一些功能相似的控件,而且这些控件可能是容器
po采用了类的方法,下面以动态生成元器件为例:
1.首先先做出一个panel,里面可能有picturebox,button等控件,这都没关系,先做出来可以方便我们后面写构造函数

2.接下来写一个类,这些控件都作为成员变量,控件名建议与之前创建出来的panel同名

3.如果我们还想给组件添加事件,只要在构造函数里加上,然后在类里添加这些事件的实现

4.还可以做出一些提高功能,在这个类里有另一个类的对象(如果掌握了C++的类应用,这些功能应该不是很困难)

下面是做出来的效果,可以实现批量添加在这里插入图片描述

public class PanelComponent
        {
            public string classname;
            public Panel panelC = new Panel();
            public Label name = new Label();
            public Label value = new Label();
            private IconButton currentBtn;
            public PictureBox component = new PictureBox();
            public Panelset panelset = new Panelset(null);
            public PanelComponent(object senderbtn)
            {
                currentBtn = (IconButton)senderbtn;
                this.panelC.Controls.Add(this.value);
                this.panelC.Controls.Add(this.name);
                this.panelC.Controls.Add(this.component);
                this.panelC.Location = new System.Drawing.Point(500, 200);
                //this.panelC.Name = "panelComp";
                this.panelC.Size = new System.Drawing.Size(97, 98);
                this.panelC.TabIndex = 3;
                //this.panelC.Visible = true;
                //picturebox
                this.component.BackColor = System.Drawing.Color.Transparent;
                this.component.Dock = System.Windows.Forms.DockStyle.Fill;
                if(currentBtn.Text == "电阻")
                {
                    this.component.Image = global::window1.Properties.Resources.res;
                    classname = "电阻";
                }
                else if(currentBtn.Text == "电容")
                {
                    this.component.Image = global::window1.Properties.Resources.cap_black;
                    classname = "电容";
                }
                else if (currentBtn.Text == "电感")
                {
                    this.component.Image = global::window1.Properties.Resources.ind_black;
                    classname = "电感";
                }
                else if (currentBtn.Text == "电源")
                {
                    this.component.Image = global::window1.Properties.Resources.pow_black;
                    classname = "电源";
                }
                else  if (currentBtn.Text == "二极管")
                {
                    this.component.Image = global::window1.Properties.Resources.dio_black;
                    classname = "二极管";
                }
                this.component.Location = new System.Drawing.Point(0, 0);
                //this.component.Name = "Component";
                this.component.Size = new System.Drawing.Size(97, 98);
                this.component.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                this.component.TabIndex = 2;
                this.component.TabStop = false;
                this.component.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.component_MouseDoubleClick);
                this.component.MouseDown += new System.Windows.Forms.MouseEventHandler(this.component_MouseDown);
                this.component.MouseMove += new System.Windows.Forms.MouseEventHandler(this.component_MouseMove);
                // 
                // value
                // 
                this.value.AutoSize = true;
                this.value.Dock = System.Windows.Forms.DockStyle.Bottom;
                this.value.Location = new System.Drawing.Point(0, 80);
                //this.value.Name = "value";
                this.value.Size = new System.Drawing.Size(62, 18);
                this.value.TabIndex = 4;
                this.value.Text = "value";
                // 
                // name
                // 
                this.name.AutoSize = true;
                this.name.Dock = System.Windows.Forms.DockStyle.Top;
                this.name.Location = new System.Drawing.Point(0, 0);
                //this.name.Name = "name";
                this.name.Size = new System.Drawing.Size(62, 18);
                this.name.TabIndex = 3;
                this.name.Text = "name";
            }
            public void AddSet(Panelset sender)
            {
                panelset = sender;
            }
            Point pt;

            private void component_MouseDown(object sender, MouseEventArgs e)
            {
                pt = Cursor.Position;
            }
            private void component_MouseMove(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int px = Cursor.Position.X - pt.X;
                    int py = Cursor.Position.Y - pt.Y;
                    panelC.Location = new Point(panelC.Location.X + px, panelC.Location.Y + py);
                    pt = Cursor.Position;
                }
            }
            private void component_MouseDoubleClick(object sender, MouseEventArgs e)
            {
                panelset.panelSet.Visible = true;
            }
        }
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值