我设置了两个窗体
在第一个窗体上点击按钮触发创建新的控件   新创建的空间显示在第二个窗体上
我想动态创建一个Button  然后利用这个Button清除动态创建的空间  连Button也清除
我知道可以用this.Control.Remove()  
可是我不知道怎么用  里面的参数该怎么填我的第一个窗体的代码如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 实验指导9_4
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            if (checkBox1.Checked == true)         //动态创建一个label控件
            {
                Label l1 = new Label();
                l1.Location = new Point(24, 8);
                l1.AutoSize = true;
                l1.Text = "new Label";
                f1.Controls.Add(l1);
            }
            if (checkBox2.Checked == true)         //动态创建一个TextBox控件
            {
                TextBox t1 = new TextBox();
                t1.Location = new Point(24, 32);
                t1.Size = new Size(72, 40);
                t1.Text = "new TextBox";
                f1.Controls.Add(t1);
            }
            if (checkBox3.Checked == true)         //动态创建一个Button控件
            {
                Button b1 = new Button();
                b1.Location = new Point(24, 64);
                b1.Size = new Size(48, 24);
                b1.Text = "移除";
                f1.Controls.Add(b1);
                b1.Click+=new EventHandler(f1.remo);
            }
            if (checkBox4.Checked == true)            //动态创建一个CheckBox控件
            {
                CheckBox c1 = new CheckBox();
                c1.Location = new Point(24, 96);
                c1.Size = new Size(104, 40);
                c1.Text = "new CheckBox";
                f1.Controls.Add(c1);
            }
            f1.Show();
            
        }
    }
}
第二个窗体的代码如下namespace 实验指导9_4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void Form1_Load(object sender, EventArgs e)
        {        }
        public void remo(object sender, EventArgs e)
        {
            this.Controls.Remove();
            this.Show();
        }
    }
}可是不知道怎么用那个this.Control.Remove()
要实现的效果是点击动态添加的Button可以清除动态创建的控件
本人初学者   可能上面的代码有错误   还希望各位高人指导指导  

解决方案 »

  1.   

      不能用Remove..   要用Dispose,这样才能把资源清理干净...
      

  2.   

    给你个简单的例子吧:private void button1_Click(object sender, EventArgs e)
    {
        Button b = new Button();
        b.Text = "Button";
        Controls.Add(b);
    }private void button2_Click(object sender, EventArgs e)
    {
        Controls.Remove(b);
    }编译出错,因为b的生命周期不对,所以应该:
    Button b = new Button();
    private void button1_Click(object sender, EventArgs e)
    {
        b.Text = "Button";
        Controls.Add(b);
    }private void button2_Click(object sender, EventArgs e)
    {
        Controls.Remove(b);
    }同理,注意声明周期,你的问题也可以解决,不知道你明白我的意思么
      

  3.   

    本人刚学C# 不久 所以很多不懂 
    我在编译的时候Controls.Remove(b);
    说“b”不存在  可是我明明声明了
      

  4.   

    如果是当前窗体调用另外窗体,那就把要删除空间的窗体在当前窗体上声明个成员变量,然后在当前窗体调用该成员变量去删除

    class FormCurrent
    {
     frmOther f = new frmOther();  void fun()
      {
        f.Controls.Remove(***);
      }
    }
      

  5.   

    没办法,写完整了,你看一下吧:using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication15
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public static Button b = new Button();
            private void button1_Click(object sender, EventArgs e)
            {            b.Text = "Button";
                Form2 f2 = new Form2();
                f2.Controls.Add(b);
                f2.Show();
            }
        }
    }using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication15
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Controls.Remove(Form1.b);
            }
        }
    }
      

  6.   

    namespace 实验指导9_4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {        }
            public void remo(object sender, EventArgs e)
            {
                //this.Controls.Remove();
                //改为:
                this.Controls.Clear();
                this.Show();
            }
        }
    }