开发者

How to remove Parent of Control

开发者 https://www.devze.com 2022-12-25 17:02 出处:网络
I am removing button control from one list to another listh and followin开发者_Go百科g error occures

I am removing button control from one list to another listh and followin开发者_Go百科g error occures

"Specified element is already the logical child of another element. Disconnect it first"

any idea how to remove that exception.


Should be fairly easy:

  1. Get the list of controls from your parent control.
  2. Call the Remove function on that list to remove your control.

So something like this:

myListControl.Controls.Remove(myControlToRemove);


Try this:

public partial class Form1 : Form
{
        private void button1_Click(object sender, EventArgs e)
        {
            Util.PlaceControlToContainer(this.button1, this.panel2);
        }
}
public static class Util
{
   public static void PlaceControlToContainer(Control control, Control container)
   {
       lock (control)
       {
          if (control.Parent != null)
          {
              control.Parent.Controls.Remove(control);
          }
          container.Controls.Add(control);
       }
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消