C# 跨线程 加载控件 【百般无奈.】 (TabPage.Controls.Add(Control ))
我在 主线程Form1中new 了一个 静态按钮 然后开了两个子线程 分别打开 Form2 和Form3 我想要在这两个线程中的窗体里面加载 Form1中的静态按钮 即 方法 TabPage.Controls.Add(Control )。想要达到的效果是 :只有一个按钮 无论是Form2还是Form3 要加载按钮时 都是加载的Form1中的那一个静态按钮,代码如下:加载控件时 都是用的Invoke 方法,但是 还是报错 “”
Form1:
public partial class Form1 : FormForm2 和Form3 一样
{
public Form1()
{
InitializeComponent();
bt.Text = "123";
}
static Button bt = new Button();
Form2 f2 = new Form2();
Form3 f3 = new Form3();
private void button1_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(
() =>
{
f2.eve += new EventHandler(f_eve);
f2.ShowDialog();
}
)).Start();
}
private void button2_Click(object sender, EventArgs e)
{
new Thread(new ThreadStart(
() =>
{
f3.eve += new EventHandler(f_eve);
f3.ShowDialog();
}
)).Start();
}
void f_eve(object sender, EventArgs e)
{
TabPage t = (TabPage)sender;
t.Invoke(new EventHandler(delegate
{
t.Controls.Add(bt);
}));
}
}
--------------------编程问答-------------------- http://www.cnblogs.com/mokey/articles/2095457.html --------------------编程问答-------------------- --------------------编程问答-------------------- 所谓的"静态"按钮没有实际意义,犯不着跟错误的设计碰得"百般无奈"。
补充:.NET技术 , C#