如何双击在运行时 C# 添加的控件打开新窗口

本文关键字:控件 新窗口 窗口 何双击 双击 运行时 添加 | 更新日期: 2023-09-27 18:34:50

我正在我的应用程序中添加运行时可移动的 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();  
}

如何双击在运行时 C# 添加的控件打开新窗口

我明白了。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();
    }