这是小姐。我单击一个按钮并创建一个新的PictureBox没问题。当我单击并拖动时,我将图片移动到它的新位置。现在,当我再次单击该按钮时,我创建了同一PictureBox的新实例,当我尝试移动旧实例时,我最终移动了新创建的框。我认为这是因为它们都有相同的名称:
PictureBox pic = new PictureBox();
如何通过点击在两个图片框之间切换?
*的 更新 * 感谢Nilotpal的回答,我设法解决了上述问题。唯一的问题是图片框现在似乎在摇晃,或者在另一个实例和我拖动的实例之间切换回位置和第四位。不管怎样,我真的不确定如何解决这个问题。有什么想法吗?
*的 更新 * 我的代码:
private void code128ToolStripMenuItem_Click(object sender, EventArgs e)
{
bNum++;
Barcode barcode = new Barcode();
pic = new PictureBox();
pic.Name = "bCode" + bNum;
pic.SizeMode = PictureBoxSizeMode.AutoSize;
pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");
pic.Show();
labelHolder.Controls.Add(pic);
pic.BringToFront();
pic.MouseDown += pic_MouseDown;
pic.MouseMove +=pic_MouseMove;
pic.MouseUp += pic_MouseUp;
}
PictureBox thisPB;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldX = e.X;
oldY = e.Y;
}
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
thisPB = (PictureBox)sender;
thisPB.Location = new Point(pic.Location.X - (oldX - e.X), pic.Location.Y - (oldY - e.Y));
this.Refresh();
}
}
private void pic_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
答案 0 :(得分:2)
private void button1_Click(object sender, EventArgs e)
{
PictureBox pb = new PictureBox();
pb.Top = 200;
pb.Left = 200;
pb.BackColor = Color.Gray;
pb.MouseMove += new MouseEventHandler(pb_MouseMove);
this.Controls.Add(pb);
}
void pb_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox thisPB = (PictureBox)sender;
thisPB.Left = e.X;
thisPB.Top = e.Y;
}
}
此举将不稳定,您可以根据需要进行更改。
答案 1 :(得分:2)
修正了它!
旧代码:
Barcode barcode = new Barcode();
pic = new PictureBox();
pic.Name = "bCode" + bNum;
pic.SizeMode = PictureBoxSizeMode.AutoSize;
pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");
pic.Show();
labelHolder.Controls.Add(pic);
pic.BringToFront();
pic.MouseDown += pic_MouseDown;
pic.MouseMove +=pic_MouseMove;
pic.MouseUp += pic_MouseUp;
}
PictureBox thisPB;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldX = e.X;
oldY = e.Y; }
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
thisPB.Location = new Point(pic.Location.X - (oldX - e.X), pic.Location.Y - (oldY - e.Y));
this.Refresh();
}
}
private void pic_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
工作代码:
Barcode barcode = new Barcode();
pic = new PictureBox();
pic.Name = "bCode" + bNum;
pic.SizeMode = PictureBoxSizeMode.AutoSize;
pic.Image = barcode.createBarcode(BarcodeLib.TYPE.CODE128, 300, 100, "123456789");
pic.Show();
labelHolder.Controls.Add(pic);
pic.BringToFront();
pic.MouseDown += pic_MouseDown;
pic.MouseMove +=pic_MouseMove;
pic.MouseUp += pic_MouseUp;
}
PictureBox thisPB;
private void pic_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
oldX = e.X;
oldY = e.Y;
}
private void pic_MouseMove(object sender, MouseEventArgs e)
{
if(mouseDown)
{
thisPB = (PictureBox)sender;
thisPB.Location = new Point(thisPB.Location.X - (oldX - e.X), thisPB.Location.Y - (oldY - e.Y));
this.Refresh();
}
}
private void pic_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
答案 2 :(得分:1)
您可能会觉得这很有帮助。
static class ExtensionMethods
{
public static Point Add(this Point original, Point value)
{
return new Point(original.X + value.X, original.Y + value.Y);
}
public static Point Subtract(this Point original, Point value)
{
return new Point(original.X - value.X, original.Y - value.Y);
}
}