我想在另一个线程里添加控件,用委托怎么实现??下面是源码,按着没反应[C#] 纯文本查看 复制代码 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 跨线程修改控件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
addPic();
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
public delegate void Pic1add();
public void addPic()
{
PictureBox temp = new PictureBox();
temp.Size = new System.Drawing.Size(179, 118);
temp.Location = new System.Drawing.Point(385, 54);
temp.TabIndex = 4;
temp.TabStop = false;
if (pictureBox1.InvokeRequired)
{
Pic1add ap = new Pic1add(delegate()
{
pictureBox1.Controls.Add(temp);
});
pictureBox1.Invoke(ap);
}
else
{
pictureBox1.Controls.Add(temp);
}
}
}
}
|