日期:2014-05-17  浏览次数:20834 次

我需要在界面上动态加载N多的控件,想在线程中创建控件,并添加到主界面上,怎么解决!急!
如题所示,请大神帮我解决!
.net

------解决方案--------------------
  public delegate void AddControlHanddler();

        private void Form1_Load(object sender, EventArgs e)
        {
            new System.Threading.Thread(new System.Threading.ThreadStart(AddControlThread)).Start();
        }

        private void AddControlThread()
        {
            AddControlHanddler add = new AddControlHanddler(AddControlFunc);
            this.Invoke(add);
        }

        //线程动态创建100个按钮
        private void AddControlFunc()
        {
            for (int i = 0; i < 100; i++)
            {
                Button b = new Button();
                b.Text="按钮" + (i+1).ToString();
                b.Location = new Point(10, 10 + 30 * i);
                b.Size = new Size(75, 24);
                this.Controls.Add(b);
            }
        }