Reputation: 1186
I have created flowlayoutpanel dynamically in windows form using c# . An i have added one button in that panel . Can anyone tell me how to remove that panel dynamically after the button as pressed? Here is the coding :
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]);
}
Upvotes: 1
Views: 2437
Reputation: 8654
I expect that you keep FlowLayoutPanel[] flws
in a form.
In the same way you added flws
to the Form by this.Controls.add(flws)
, remove it in the same way: this.Controls.Remove(flws)
.
Remove it when the button is clicked, add the Clicked
event to your form:
pubic class Form1: Form {
FlowLayoutPanel[] flws;
// ...
private void button_Click(object sender, EventArgs e) {
this.Controls.Remove(flws);
}
}
Upvotes: 0
Reputation:
Came up with this really quick, hope it helps.
[Edited it to meet your requeriments].
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));
}
}
}
Upvotes: 1
Reputation: 137547
Note that FlowLayoutPanel
derives from Control
which is IDisposable
. This means you should call Dispose
on the panel when you remove it:
private void RemovePanel(FlowLayoutPanel panel) {
this.Controls.Remove(panel);
panel.Dispose();
}
You don't need to worry about the Button
s you've added to the panel, because
When you call
Dispose
on the form, it will callDispose
for each control in itsControls
collection.
Upvotes: 1