C#批量动态生成控件
程序员文章站
2024-02-18 16:19:46
...
有时候我们需要动态添加一些功能相似的控件,而且这些控件可能是容器
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;
}
}