-1

我在运行时在按钮的单击事件上添加了一个面板,即每次我们单击该按钮时都会调用该面板。

Panel p = new Panel();
p.Location = new Point(10, (childrenCount - 1) * 280);
p.Width = 800;
p.Height = 250;
/****some code****/
this.Controls.Add(p);

现在我在我的表单上放置了一个新按钮,我希望在表单的点击事件中删除面板。那么如何在此按钮的单击事件上调用此面板类的对象 p。

以及如何在运行时向面板添加背景 这是我要在其中添加背景的面板

public void addchildbtn_Click_1(object sender, EventArgs e)
       { //Adds a child
           try
           {
               rmvchildclicklbl.Enabled = true;
               remvchildbtn.Enabled = true;
               no_childnumlbl.Text = (childrenCount).ToString();
               childclmns clmn = new childclmns();
               clmn.Location = new Point(100, 200);
               clmn.Name = "column" ;
               this.Controls.Add(clmn);
               childrenCount++;
               Panel p = new Panel();
               p.Location = new Point(10, (childrenCount - 1) * 280);
               p.Width = 800;
               p.Height = 250;
               p.BorderStyle = BorderStyle.Fixed3D;
               p.ForeColor = Color.Black;
               p.BackColor = Color.WhiteSmoke;
               p.BackgroundImage = new Bitmap(@"image\baby.bmp");
               p.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
               // p.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
               Label lblchild = new Label();
               lblchild.BorderStyle = BorderStyle.Fixed3D;
               lblchild.Location = new Point(20, 50);
               lblchild.Size = new System.Drawing.Size(50, 20);
               lblchild.Text = "Child  " + (childrenCount - 1);
               TextBox tName = new TextBox(); //TextBox for the name
               tName.BorderStyle = BorderStyle.Fixed3D;
               tName.Location = new Point(120, 50);
               tName.Size = new System.Drawing.Size(135, 60);
               tName.Text = "";
               MonthCalendar calendar = new MonthCalendar(); //Calendar to choose the birth date
               calendar.Location = new Point(310, 40);
               ComboBox bloodGroup = new ComboBox(); //ComboBox for the blood group
               bloodGroup.Location = new Point(650, 50);
               bloodGroup.Size = new System.Drawing.Size(135, 60);
               for (Enum.Blood_Group l = Enum.Blood_Group.O_negative; l <= Enum.Blood_Group.AB_positive; l++)
               {
                   bloodGroup.Items.Add(l);
               }
               p.Controls.Add(lblchild);
               p.Controls.Add(tName);
               p.Controls.Add(calendar);
               p.Controls.Add(bloodGroup);
               this.Controls.Add(p);
               p.Name = "panel" + childrenCount;
               panels.Add(p);
               //Summit button location
               button1.Visible = true;
               int j=350, k= 300;
               k = k + ((childrenCount - 1) * 280);
               button1.Location = new Point(j, k);
               btnht= k.ToString();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }

       }
4

1 回答 1

1

您需要对要删除的面板的引用。

有很多方法可以做到这一点。

您可以向后搜索Controls集合并删除最后添加的面板,或者例如,将添加的面板存储在某处的列表中,以便您可以将其删除:

// in the form class
List<Panel> panels = new List<Panel>();

// in your add button click event
/****some code****/
panels.Add(p);
this.Controls.Add(p);

// in your remove button click event
if (panels.Any())
{
    var panel = panels.Last();
    this.Controls.Remove(panel)
    panels.Remove(panel);
}

或者,或者:

// in your add button click event
Panel p = new Panel();
p.Location = new Point(10, (childrenCount - 1) * 280);
p.Width = 800;
p.Height = 250;
p.Name = "panel" + childrenCount;
/****some code****/
this.Controls.Add(p);


// in your remove button click event
this.Controls.RemoveByKey("panel" + childrenCount);
childrenCount--;
于 2013-04-29T14:00:08.623 回答