Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello friends.
this is my question.
i have a form and a user control and a flowlayoutpanel.
i have a button on the form to name of "add".
As you see i have a button on user control.it S name is del.

http://upload7.ir/viewer.php?file=30884462722471978649.jpg[^]
when i click add button_ one user control add to my flowlayoutpanel and so on.
i want that when i click on delete button on user control _the same user control remove from
my flowlayoutpanel and so on.
this is my code for delete and add button.
add:this work fine.

C#
int counter;
      private void button1_Click(object sender, EventArgs e)
      {
          counter++;
          if (counter == 10) { counter = 0; }
          UserControl1 btnAdd = new UserControl1();

        //  this.Text = counter.ToString();
          // btnAdd.Name = "click" + counter.ToString();
          btnAdd.Name = "usercontrol" + counter.ToString();
          btnAdd.button1.Name = "click";

         // btnAdd.Click += new EventHandler(btnAdd_Click);
          btnAdd.button1.Click += new EventHandler(btnAdd_Click);
          btnAdd.button1.BackColor = Color.Gray;
          btnAdd.button1.Text = "Add";

          //  btnAdd.Location = new System.Drawing.Point(20, 20);

          flowLayoutPanel1.Controls.Add(btnAdd);
      }


delete:this dont work.
C#
private void button1_Click(object sender, EventArgs e)
       {
           UserControl1 btn = new UserControl1();

          // Button btn = (Button)sender;
           Form4 hh=new Form4();
           // Form4 gg = new Form4();
           if (btn != null)
           {
           hh.flowLayoutPanel1.Controls.RemoveByKey(btn.Name);
              // flowLayoutPanel1.Controls.RemoveByKey(btn.Name);
           this.Text = (btn.Name + " removeded");
           }

       }
Posted
Updated 24-Apr-13 20:51pm
v2

1 solution

I think I know why this does not work. You tried to remove the delete button from the flowlayout. But you want to remove the UserControl1 from the flowlayout.
Try something like:
C#
private void button1_Click(object sender, EventArgs e)
{
	Button btn = sender as Button;
	if (btn != null)
	{
		flowLayoutPanel1.Controls.RemoveByKey(btn.Parent.Name);
		this.Text = (btn.Name + " removeded");
	}
}

Or if the delete button is in a panel you might need "Parent.Parent.Name" - and so on. I think the RemoveByKey is not recursive!

EDIT:
And of course you need to change your event registering from:
C#
btnAdd.button1.Click += new EventHandler(btnAdd_Click);

to
C#
btnAdd.button1.Click += new EventHandler(button1_Click);

otherwise you will recursivly add controls to your flowpanel.
 
Share this answer
 
v2
Comments
mahmoodof 25-Apr-13 3:33am    
thanks alot.it work fine.
one another question.
if i want have a ADD button on my user control not on my form.
how change does my code make?
toATwork 25-Apr-13 3:36am    
You have 2 possibilites. Either handle the add in your control and add a new UserControl1 to the (parent) flowpanel there. Or call the add functionality from the main form.
mahmoodof 25-Apr-13 3:44am    
ok.but how i handle addbutton in my control .
i am new in c#.
do you have a sample for me.
thanks.
toATwork 25-Apr-13 4:31am    
Sample is rather difficult.
You could change the constructor of your UserControl1 that it requieres a variable with the type of your parent control.
In your parent control define a public method which handles all the adding of new controls.
Then you can simply call in your control the function of the main (parent) control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900