c#,winform 大侠帮我看个问题很郁闷。

来源:百度知道 编辑:UC知道 时间:2024/06/15 22:22:03
Form frm = 一个form对象;
frm.show();//报错:线程间操作无效: 从不是创建控件“dataGridView1”的线程访问它。

我是在做一个导航,点某个导航的时候。就把窗体填充到容器里,然后show出来。其他窗体都没问题,就俩窗体脑袋上长刺了,死活不行。

你需要把你的那段代码放在那个线程之上,比如说: private void AddList(object text)
{
//判断对方控件是否调用了invoke方法
if (this.listBox1.InvokeRequired)
{
SetSaft d = new SetSaft(AddList);
this.Invoke(d, new object[] { text });
}
else
{
this.listBox1.Items.Add(text);
}
}
详细内容可以看看这个
http://hi.baidu.com/haifeng_4216/blog/item/0febc7244f7ec43ac995595e.html

Windows窗体是严格按照线程原理运行的,每个窗体句柄只能被其独有的线程所访问,这是一种安全检验。如果希望不进行这种检验,可以在窗体的构造函数之下加一行代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false;
}
}
去试试吧。