c#在线程中动态生成控件,求代码,该怎么处理
c#在线程中动态生成控件,求代码
看了一些列子,发现似懂非懂,但是现在比较着急做出东西,只好厚颜无耻的要代码了。
听说在线程中创建UI控件需要委托。
public void a()
{
System.String[][] str = list.ToArray();
for (int k = 0; k < str.Length;k++ )
{ int S = 31;
PictureBox picBox = new PictureBox();
Label la = new Label();
}
}
private void button4_Click_2(object sender, EventArgs e)
{
Button bnBox1 = (Button)sender;
Thread newThread = new Thread(this.DoWork));
newThread.start();
}
然后就提示错误,请问该怎么做,请出示代码,不胜感激。
------解决方案--------------------
public void a()
{
System.String[][] str = list.ToArray();
for (int k = 0; k < str.Length;k++ )
{ int S = 31;
this.Invoke(new Action(() => {
PictureBox picBox = new PictureBox();
picBox.Location = 位置
picBox.Size = 大小
picBox.Name = "picBox" + k;
Controls.Add(picBox);
Label la = new Label();
... //类似
}));
}
}
看了一些列子,发现似懂非懂,但是现在比较着急做出东西,只好厚颜无耻的要代码了。
听说在线程中创建UI控件需要委托。
public void a()
{
System.String[][] str = list.ToArray();
for (int k = 0; k < str.Length;k++ )
{ int S = 31;
PictureBox picBox = new PictureBox();
Label la = new Label();
}
}
private void button4_Click_2(object sender, EventArgs e)
{
Button bnBox1 = (Button)sender;
Thread newThread = new Thread(this.DoWork));
newThread.start();
}
然后就提示错误,请问该怎么做,请出示代码,不胜感激。
------解决方案--------------------
public void a()
{
System.String[][] str = list.ToArray();
for (int k = 0; k < str.Length;k++ )
{ int S = 31;
this.Invoke(new Action(() => {
PictureBox picBox = new PictureBox();
picBox.Location = 位置
picBox.Size = 大小
picBox.Name = "picBox" + k;
Controls.Add(picBox);
Label la = new Label();
... //类似
}));
}
}