如何识别c#中动态创建的面板



我正在c#中执行一个Windows窗体应用程序,在该应用程序中,我在"panel_Side"中每次单击都会创建一个面板。但我的问题是,当我必须选择一个特定的小组。我不知道如何识别每个面板。此外,由于同样的原因,我在拖放时也遇到了问题。

public void Coloco_Figura(string figura, int Punto_X, int Punto_Y)
   {
       panel_foto = new Panel();
       panel_foto.BackColor = Color.Transparent;  //saco fondo
       panel_foto.BackgroundImage = Image.FromFile(figura);    //asigno imagen al panel
       panel_foto.BackgroundImageLayout = ImageLayout.Stretch;
       panel_foto.Size = new Size(45, 45);
       panel_foto.Location = new Point(Punto_X - 10, Punto_Y - 10);
       panel_foto.BringToFront();
       panel1.SendToBack();
       panel_inside.Controls.Add(panel_foto);
       dame_x = Punto_X;

       this.panel_foto.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MouseDown);
       this.panel_foto.MouseUp += new System.Windows.Forms.MouseEventHandler(this.MouseUp);
       this.panel_foto.MouseMove += new System.Windows.Forms.MouseEventHandler(this.MouseMove);
   }

以下是鼠标事件

private void MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
   {
       #                   region Borrar  Notas
       if (Estado_Borro == true)
       {
           foreach (Panel p in panel_inside.Controls)
           {
               if (p is Panel)
               {
                   panel_inside.Controls.Remove(p);
                   if (panel_inside.Controls.Count == 0)
                   {
                       listanotas.Clear();
                   }
                   else
                   {
                       for (int i = 0; i < listanotas.Count; i++)
                       {
                           Notas nota = listanotas[i];
                           if (nota.posX == dame_x)
                           {
                               listanotas.Remove(nota);
                           }
                       }
                   }
               }
           }

           Estado_Borro = false;
       }
        if (e.Button == MouseButtons.Left)
       {
           drag = true;
           x = e.X;
           y = e.Y;
       }
   }
      public void MouseUp(object sender,                       System.Windows.Forms.MouseEventArgs e)
   {
       drag = false;
   }

   public void MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
   {
       if (drag)
       {
           this.panel_foto.Location = new Point(Cursor.Position.X-this.Left, Cursor.Position.Y-this.Top);               
       }

要识别每个面板,首先必须设置Name或Tag属性:
panel_foto.Name = "panel_foto1";

然后在事件处理程序中,执行以下操作:

Panel p=(Panel)sender;
if (p.Name == "panel_foto1")
{
     //Do your staff
}

相关内容

  • 没有找到相关文章

最新更新