單擊button1在panel上動態新建了多個label,現在想要點選選擇某個動態新建的label,按button2,可以把這個label刪掉在button1_Click事件中,建立label,程式碼:Labellb1=newLabel(); lb1.Name="panel"+j; lb1.BackColor=Color.Transparent; lb1.BorderStyle=BorderStyle.FixedSingle; Panel1.Controls.Add(lb1);為這些label增加Click事件 Labellb1=newLabel(); lb1.Name="panel"+j; lb1.BackColor=Color.Transparent; lb1.Click+=newEventHandler(label_Click); lb1.BorderStyle=BorderStyle.FixedSingle;Panel1.Controls.Add(lb1);stringname="";privatevoidlabel_Click(objectsender,EventArgse){ Labellbl=senderasLabel; name=lbl.Name;}然後在Button2的Click中:privatevoidbutton2_Click(objectsender,EventArgse){ Labellbl=Panel1.Controls[name]; if(lbl!=null) Panel1.Controls.Remove(lbl);}
單擊button1在panel上動態新建了多個label,現在想要點選選擇某個動態新建的label,按button2,可以把這個label刪掉在button1_Click事件中,建立label,程式碼:Labellb1=newLabel(); lb1.Name="panel"+j; lb1.BackColor=Color.Transparent; lb1.BorderStyle=BorderStyle.FixedSingle; Panel1.Controls.Add(lb1);為這些label增加Click事件 Labellb1=newLabel(); lb1.Name="panel"+j; lb1.BackColor=Color.Transparent; lb1.Click+=newEventHandler(label_Click); lb1.BorderStyle=BorderStyle.FixedSingle;Panel1.Controls.Add(lb1);stringname="";privatevoidlabel_Click(objectsender,EventArgse){ Labellbl=senderasLabel; name=lbl.Name;}然後在Button2的Click中:privatevoidbutton2_Click(objectsender,EventArgse){ Labellbl=Panel1.Controls[name]; if(lbl!=null) Panel1.Controls.Remove(lbl);}