【发布时间】:2018-08-20 23:22:33
【问题描述】:
在我的代码中,每次按下 button1 时,都会在面板中生成一个名为 NOT 的图片框实例。单击并按住图像时,可以拖动图像。我的问题是每次按下 button1 时,我都希望创建另一个具有相同属性的图片框,这样理论上我可以整天按下 button1 并随意拖动任意数量的非图片框对象。到目前为止,一旦按下按钮,就只创建了一个 NOT 实例,并且无法生成另一个实例。所以本质上如何在每次按下 button1 时创建 NOT 的新唯一实例。
public Form1()
{
InitializeComponent();
Drag();
}
private void button1_Click(object sender, EventArgs e)
{
spawnGate("not");
}
PictureBox NOT = new PictureBox();
private Point startPoint = new Point();
public void Drag()
{
NOT.MouseDown += (ss, ee) =>
{
if (ee.Button == System.Windows.Forms.MouseButtons.Left)
{
startPoint = Control.MousePosition;
}
};
NOT.MouseMove += (ss, ee) =>
{
if (ee.Button == System.Windows.Forms.MouseButtons.Left)
{
Point temp = Control.MousePosition;
Point res = new Point(startPoint.X - temp.X, startPoint.Y - temp.Y);
NOT.Location = new Point(NOT.Location.X - res.X, NOT.Location.Y - res.Y);
startPoint = temp;
}
};
}
public void spawnGate(string type)
{
switch (type)
{
case "not":
NOT.Width = 100;
NOT.Height = 50;
NOT.Image = Properties.Resources.Not_gate;
NOT.SizeMode = PictureBoxSizeMode.Zoom;
workspace.Controls.Add(NOT);
break;
}
}
}
【问题讨论】:
标签: c# winforms instance picturebox