是异常
未处理的“System.ArgumentException”类型的异常出现在 system.windows.forms.dll 中其他信息:在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。
未处理的“System.ArgumentException”类型的异常出现在 system.windows.forms.dll 中其他信息:在某个线程上创建的控件不能成为在另一个线程上创建的控件的父级。
解决方案 »
- C# 窗体上 Label控件 实时刷新 显示当前时间
- 远程服务器验证会很慢吗?
- 怎样从gridview导入数据到excel里
- 问个checkedListBox选中数据的问题,在线等
- C# winform 强大的JS/VBS脚本引擎
- System.Web.HttpContext.Current.Server.MapPath("") 这句总是出现"未将对象引用设置到对象的实例"异常呢?
- js 调用 c# 函数问题
- 在 sqlserver查询分析器中 用哪条命令查看一个表的结构 或者索引等其它对象
- 用Remoting得到一个远程Form对象,show()他的时候不行
- .net 4.0调试出现 ,签名不匹配。
- C#中日期类如何实现
- 两样东西~一样100分~
this.panel1.Controls.Add(newLabel);
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());
}
}
}
问题是如何解决: 用另外一个线程把一个控件添加到panel中
来创建线程,这个实际上就是通代理来创建.然后开一新线程来创建多
个控件,我找了几个例子,但没有找到一个是用一个主线程来创建父控件
而又一个子线程来创建子控件的例子.你看他的帖子上面是用到了一个
panel.根据我对句柄映射表的理解,这是个对线程的局部表.那么子线程
是无法访问到父控件的句柄那么,也就是说this.panel1.Controls.Add (new Button());这一句调用是不成立的.因为没法访问到给句柄,当然你说用Invoke
那样可以撇开该问题.我想确定一下我的说法对不对,可否赐教?谢谢!