调用类的返回值,实际上在类里面执行的是个线程,在线程里面打日志出来,所以要这样用,后面线程的时候就说是 非创建线程 不能更改 一下是form2的代码 public partial class Form2 : Form { TextBox txtBox; public Form2(TextBox txt) { InitializeComponent(); txtBox = txt; } Class1 ad = new Class1(); Form1 d = new Form1(); private void button1_Click(object sender, EventArgs e) {
Form2 frm = new Form2(this.textBoxA);
frm.Show();第2个窗体的构造函数里面:
TextBox txtBox;
public Form2(TextBox txt)
{
InitializeComponent();
txtBox = txt;
}
第2个窗体的按钮B的点击事件里:
txtBox.Text = 调用类的返回值;
调用类的返回值,实际上在类里面执行的是个线程,在线程里面打日志出来,所以要这样用,后面线程的时候就说是 非创建线程 不能更改
一下是form2的代码
public partial class Form2 : Form
{
TextBox txtBox;
public Form2(TextBox txt)
{
InitializeComponent();
txtBox = txt;
} Class1 ad = new Class1();
Form1 d = new Form1();
private void button1_Click(object sender, EventArgs e)
{
ad.dd();
} private void Form2_Load(object sender, EventArgs e)
{
ad.myevent += new Class1.mydelegate(givevalue);
} public void givevalue(string imgname)
{
txtBox.Text=imgname;
}
以下是class类的代码
public class Class1
{
public delegate void mydelegate(string imgname); //1、定义委托
public event mydelegate myevent; //1、定义事件(使用委托命名的类型) public void dd()
{
Thread d = new Thread(this.ddd);
d.IsBackground = true;
d.Start();
}
private void ddd()
{
myevent("12345");
Thread.Sleep(10000);
}
随意写的,没有太规范,再请教一下,谢谢
private void button1_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.PassValueEvent += new Form2.PassValue(form_PassValueEvent);
form.ShowDialog();
} void form_PassValueEvent(string value)
{
textBox1.Text = value;
}
Form2: public delegate void PassValue(string value);
public event PassValue PassValueEvent;
private void button1_Click(object sender, EventArgs e)
{
string strReturn=Method();//调用方法;
if(PassValueEvent!=null)
{
PassValueEvent(strReturn);
}
}