我想在一个类里调用主窗体中的控件,所以采用的了委托和事件,但是在执行过程中textBox1.Text有值却不能在窗体控件中显示,请教各位指点,下面是我做的一个简单的代码,贴出来请大家帮忙看看,谢谢!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 demon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Class1 cla = new Class1();
cla.FormDisplay();
} string str = "";
public void Display(int ii)
{
textBox1.Text += ii.ToString();
} }
}Class类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace demon
{
class Class1
{ public delegate void Datadelegate(int str);
public event Datadelegate Dataevent; int[] idata = { 12, 13, 24, 15, 16, 23, 14, 10, 22, 18 };
public void Statr ()
{ for (int i = 0; i < 10;i++ )
{
if ( idata[i]< 20)
{
if (Dataevent != null)
{
Dataevent(idata[i]); }
}
}
}
public void FormDisplay()
{
Form1 f = new Form1();
Dataevent += f.Display;
Statr();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace demon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Class1 cla = new Class1();
cla.FormDisplay();
} string str = "";
public void Display(int ii)
{
textBox1.Text += ii.ToString();
} }
}Class类using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace demon
{
class Class1
{ public delegate void Datadelegate(int str);
public event Datadelegate Dataevent; int[] idata = { 12, 13, 24, 15, 16, 23, 14, 10, 22, 18 };
public void Statr ()
{ for (int i = 0; i < 10;i++ )
{
if ( idata[i]< 20)
{
if (Dataevent != null)
{
Dataevent(idata[i]); }
}
}
}
public void FormDisplay()
{
Form1 f = new Form1();
Dataevent += f.Display;
Statr();
}
}
}
Class1 cla = new Class1();
cla.Dataevent += xxxx
{
Form1 f = new Form1();//此form非彼form Dataevent += f.Display;
Statr();
}
Form1 f = new Form1();//此form非彼form
你又新开了一个form1 ,而不是程序启动时的form1一,不知你明白了没