如何从flowloayout中删除用户控件

如何从flowloayout中删除用户控件

本文介绍了如何从flowloayout中删除用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友。

这是我的问题。

i有一个表单,一个用户控件和一个flowlayoutpanel。

i有一个按钮名称为add的表单。

如您所见,我在用户控件上有一个按钮.S名称为del。



[]

当我点击添加按钮_一个用户控件添加到我的flowlayoutpanel等等。

i希望当我点击用户控件上的删除按钮_同一个用户控件从

我的flowlayoutpanel删除等等。

这是我删除和添加按钮的代码。

add:这个工作正常。



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.

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.

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");
           }

       }

推荐答案

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");
	}
}



或者如果删除按钮位于面板中,则可能需要Parent.Parent.Name - 依此类推。我认为RemoveByKey不是递归的!



编辑:

当然,您需要更改您注册的活动:


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!


And of course you need to change your event registering from:

btnAdd.button1.Click += new EventHandler(btnAdd_Click);



to


to

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



否则你会递归地向你的流程板添加控件。


otherwise you will recursivly add controls to your flowpanel.


这篇关于如何从flowloayout中删除用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:28