1

我已经使用 c# 以 Windows 形式动态创建了 flowlayoutpanel。我在该面板中添加了一个按钮。谁能告诉我如何在按下按钮后动态删除该面板?这是编码:

  FlowLayoutPanel[] flws ;
       Button[] butns ;

        for ( int i=0; i<3; i++)
          {    
            flws[i] = new FlowLayoutPanel();
            flws[i].Name = "flw" + i;
            flws[i].Location = new Point(3,brh);
            flws[i].Size = new Size(317,122);
            flws[i].BackColor = Color.DarkCyan;
            flws[i].BorderStyle = BorderStyle.Fixed3D;
            butns[i] = new Button();
            butns[i].Click += new EventHandler(butns_Click);
            butns[i].Text = "submit";
            butns[i].Name = "but" + i;
            butns[i].Location = new Point(1100, 186 + brh);

            flws[i].Controls.Add(butns[i]);
           }
4

3 回答 3

1

请注意,FlowLayoutPanel派生自Controlwhich is IDisposable。这意味着您应该Dispose在删除面板时调用它:

private void RemovePanel(FlowLayoutPanel panel) {
    this.Controls.Remove(panel);
    panel.Dispose();
}

您无需担心Button已添加到面板中的 s,因为

当您调用Dispose窗体时,它将调用其集合Dispose中的每个控件。Controls

于 2013-08-01T05:06:08.103 回答
1

很快就想到了这个,希望对您有所帮助。

[编辑它以满足您的要求]。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace FlowLayoutStackoverflow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //Load three FLP's
            for (int i = 0; i < 3; i++)
            {
                var _flowLayoutPanel = new FlowLayoutPanel();
                _flowLayoutPanel.Name = "Flow" + i;
                _flowLayoutPanel.Location = new Point(30*i, 30*i);
                _flowLayoutPanel.Size = new Size(300, 30);
                _flowLayoutPanel.BackColor = Color.DarkCyan;
                _flowLayoutPanel.BorderStyle = BorderStyle.Fixed3D;
                _flowLayoutPanel.Disposed += _flowLayoutPanel_Disposed;

                //Dispose Button
                var _button = new Button();
                _button.Text = "Dispose";
                _button.Name = "DisposeButton" + i;
                _button.Location = new Point(1*i, 1*i);
                _button.MouseClick += _button_MouseClick;

                _flowLayoutPanel.Controls.Add(_button);
                this.Controls.Add(_flowLayoutPanel);
            }
        }

        private void _button_MouseClick(object sender, MouseEventArgs e)
        {
            (sender as Button).Parent.Dispose();
        }

        //Notify disposal
        private void _flowLayoutPanel_Disposed(object sender, EventArgs e)
        {
            MessageBox.Show(string.Format("Disposed FlowLayoutPanel with name {0}", (sender as FlowLayoutPanel).Name));
        }
    }
}
于 2013-08-01T05:24:04.657 回答
0

我希望你保持FlowLayoutPanel[] flws一种形式。
以与您添加flws到表单相同的方式this.Controls.add(flws),以相同的方式将其删除:this.Controls.Remove(flws)
单击按钮时将其删除,将Clicked事件添加到表单中:

pubic class Form1: Form {
    FlowLayoutPanel[] flws;

    // ...

    private void button_Click(object sender, EventArgs e) {
        this.Controls.Remove(flws);
    }
}
于 2013-08-01T05:00:16.687 回答