我在我的应用程序中在运行时添加了一个可移动的PictureBox控件,如下面的代码所示。我希望当我双击运行时添加的PictureBox控件时,会打开一个新窗口。我试图添加双击事件,但它不起作用。你可以指导我如何在运行时双击添加的PictureBox控件时打开一个新窗口吗?
PictureBox[] PicArray;
PictureBox pb = new PictureBox();
pb.BackgroundImage = Image.FromFile(@"C:\Esp_Calculator\ESP Planner\1206570458690641104johnny_automatic_bridge.svg.med.png");
pb.Location = new Point(0, 0);
pb.BackColor = Color.Transparent;
pb.BackgroundImageLayout = ImageLayout.Zoom;
pb.MouseDown += new MouseEventHandler(this.drawArea_MouseDown);
pb.MouseMove += new MouseEventHandler(this.drawArea_MouseMove);
pb.MouseDoubleClick += pb_MouseDoubleClick;
drawArea.Controls.Add(pb);
List<PictureBox> lblList = new List<PictureBox>();
foreach (Control d in Controls)
if (d is PictureBox)
lblList.Add((PictureBox)d);
PicArray = lblList.ToArray();
private void drawArea_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
private void drawArea_MouseMove(object sender, MouseEventArgs e)
{
Control Pb = (Control)sender;
if ((Control.ModifierKeys & Keys.Control) != 0)
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{
Pb.Left += e.X - move.X;
Pb.Top += e.Y - move.Y;
}
}
}
void pb_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
Editor ed = new Editor();
ed.Show();
}
答案 0 :(得分:0)
我明白了。 DoubleClick可以完成这项工作,而不是使用MouseDoubleClick事件。谢谢 。以下是答案和工作代码:
PictureBox[] PicArray;
PictureBox pb = new PictureBox();
pb.BackgroundImage = Image.FromFile(@"C:\Esp_Calculator\ESP Planner\1206570458690641104johnny_automatic_bridge.svg.med.png");
pb.Location = new Point(0, 0);
pb.BackColor = Color.Transparent;
pb.BackgroundImageLayout = ImageLayout.Zoom;
pb.MouseDown += new MouseEventHandler(this.drawArea_MouseDown);
pb.MouseMove += new MouseEventHandler(this.drawArea_MouseMove);
pb.DoubleClick += pb_DoubleClick;
drawArea.Controls.Add(pb);
List<PictureBox> lblList = new List<PictureBox>();
foreach (Control d in Controls)
if (d is PictureBox)
lblList.Add((PictureBox)d);
PicArray = lblList.ToArray();
private void drawArea_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
private void drawArea_MouseMove(object sender, MouseEventArgs e)
{
Control Pb = (Control)sender;
if ((Control.ModifierKeys & Keys.Control) != 0)
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{
Pb.Left += e.X - move.X;
Pb.Top += e.Y - move.Y;
}
}
}
void pb_DoubleClick(object sender, System.EventArgs e)
{
Editor ed = new Editor();
ed.Show();
}