从一个动态按钮中删除2个动态按钮

时间:2018-11-30 00:15:07

标签: c# .net winforms dynamic

因此,我有一个“图像映射”,并且每次单击一个位置时,我都希望在其上显示3个按钮。这三个按钮是:热点,删除热点,保存热点。这些按钮是动态生成的。问题是,如何从删除热点按钮中删除热点,同时关闭其他2个按钮。

一些代码可以让我了解我在做什么:

private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            //Locatia
            PictureBox C = new PictureBox();
            int i = 0;
            C.Location = new Point(e.X-13, e.Y-30);
            C.Name = "Problema_" + (i + 1).ToString();
            C.ImageLocation = @"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_GPS_500px.png";
            C.Size = new Size(26, 30);
            C.SizeMode = PictureBoxSizeMode.StretchImage;
            C.BackColor = Color.Transparent;
            C.Cursor = Cursors.Hand;
            // C.Click += new EventHandler(this.StartRecordingToolStripMenuItem_Click_1);;
            PictureBox1.Controls.Add(C);

            //salveaza Locatia
            PictureBox S = new PictureBox();
            S.Name = "Salveaza_" + (i + 1).ToString();
            S.Location = new Point(e.X - 45, e.Y+10);
            S.ImageLocation = @"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Checked_Checkbox_500px.png";
            S.Size = new Size(35, 35);
            S.SizeMode = PictureBoxSizeMode.StretchImage;
            S.BackColor = Color.Transparent;
            S.Cursor = Cursors.Hand;
            PictureBox1.Controls.Add(S);

            //sterge Locatia
            PictureBox St = new PictureBox();
            St.Name = "Sterge_" + (i + 1).ToString();
            St.Location = new Point(e.X +10, e.Y+10);
            St.Cursor = Cursors.Hand;
            St.ImageLocation = @"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Close_Window_500px.png";
            St.Size = new Size(35, 35);
            St.SizeMode = PictureBoxSizeMode.StretchImage;
            St.BackColor = Color.Transparent;
            PictureBox1.Controls.Add(St);

            S.Click += new EventHandler(this.stergeAprob);
            C.Click += new EventHandler(this.clickHotspot);

        }

1 个答案:

答案 0 :(得分:0)

解决方案可能是在动态创建时也使用匿名函数动态删除它

S.Click += (o, e) => {
     //....actions
     PictureBox1.Controls.remove(C)
     //... other actions
}

由于编译器创建了内联函数,我们可以访问动态创建的变量