2015-11-20 33 views
0

我正在做一个Windows窗体应用程序在c#中,我在“Panel_Inside”中为每次点击创建一个面板。 但我的问题是,当我不得不选择一个特定的面板。我不知道如何识别每个面板。 另外,由于相同的原因,在拖放时出现问题。如何识别在c#中动态创建的面板?

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);    
     } 

回答

0

识别每个面板,首先你必须设置名称或标记属性:
panel_foto.Name = "panel_foto1";

然后在你的事件处理程序,做这样的事情:

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