动态flowlayoutpanel创建和配置在c#中

时间:2013-08-01 04:53:22

标签: c# winforms

我使用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]);
           }

3 个答案:

答案 0 :(得分:1)

请注意,FlowLayoutPanel来自Control IDisposable。这意味着您在删除面板时应该调用面板上的Dispose

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

您无需担心已添加到面板中的Button because

  

当您在表单上致电Dispose时,会为其Dispose集合中的每个控件调用Controls

答案 1 :(得分: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));
        }
    }
}

答案 2 :(得分: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);
    }
}