簡體   English   中英

如何在C#中識別動態創建的面板?

[英]How to identify dynamically created panels in c#?

我正在用c#開發Windows窗體應用程序,在其中我為“ Panel_Inside”中的每次單擊創建一個面板。 但是我的問題是何時必須選擇一個especific面板。 我不知道如何識別每個面板。 同樣由於相同的原因,我在拖放時也遇到問題。

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM