【问题标题】:Delete 2 Dynamic Buttons from one Dynamic Button从一个动态按钮中删除 2 个动态按钮
【发布时间】:2019-05-02 03:11:19
【问题描述】:

所以我有一个图像地图,每次单击某个位置时,我希望在其上显示 3 个按钮。这 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);

        }

【问题讨论】:

  • PictureBox1.Controls.Remove?

标签: c# .net winforms dynamic


【解决方案1】:

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

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

我们可以访问动态创建的变量,因为编译器会创建内联函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-08
    • 2013-03-12
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 2019-12-21
    相关资源
    最近更新 更多