是异常
未处理的“System.ArgumentException”类型的异常出现在 system.windows.forms.dll 中其他信息:在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。

解决方案 »

  1.   

    出错的语句是:
    this.panel1.Controls.Add(newLabel);
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Threading;namespace WindowsApplication2
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Panel panel1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.panel1 = new System.Windows.Forms.Panel();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(120, 24);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // panel1
    // 
    this.panel1.Location = new System.Drawing.Point(32, 80);
    this.panel1.Name = "panel1";
    this.panel1.Size = new System.Drawing.Size(216, 128);
    this.panel1.TabIndex = 1;
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {
      this.panel1,
      this.button1});
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Thread newThread = new Thread (new ThreadStart(this.AddButton));
    newThread.Start();
    }
    private void AddButton()
    {
    this.panel1.Controls.Add (new Button());
    }
    }
    }
      

  3.   

    开线程有另外的用处,原来的程序很长,所以我刚写了一个简单的程序,
    问题是如何解决: 用另外一个线程把一个控件添加到panel中
      

  4.   

    当你使用其它线程为Form创建控件的时候,可以通过Form.Invoke来确保整个创建过程最终发生在Form的主线程上,从而避免这样的错误。
      

  5.   

    你好.关于您在关于线程序,创建控件的那个帖子上,你说使用Invoke
    来创建线程,这个实际上就是通代理来创建.然后开一新线程来创建多
    个控件,我找了几个例子,但没有找到一个是用一个主线程来创建父控件
    而又一个子线程来创建子控件的例子.你看他的帖子上面是用到了一个
    panel.根据我对句柄映射表的理解,这是个对线程的局部表.那么子线程
    是无法访问到父控件的句柄那么,也就是说this.panel1.Controls.Add (new Button());这一句调用是不成立的.因为没法访问到给句柄,当然你说用Invoke
    那样可以撇开该问题.我想确定一下我的说法对不对,可否赐教?谢谢!