主窗体中的一个字符串string str;
子窗体中有一个TextBox.
Click主窗体上的一个Button1,打开子窗体.
然后子窗体的Form_Load事件中,把TextBox1.Text=str.我首先建了一个类cResult用来存储结果: public delegate void ChangeTextHandler(string s);
public class cResult
{
public event ChangeTextHandler TextChanged;
public void ChangeText(string s)
{
if (TextChanged != null)
{
TextChanged(s);
}
}
}
主窗体:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
cResult c = new cResult();
c.ChangeText("haha!");
ChildForm cf = new ChildForm(c);
cf.ShowDialog();
}
}
子窗体:
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private cResult c;
public ChildForm(cResult c)
: this()
{
this.c = c;
}
private void ChildForm_Load(object sender, EventArgs e)
{
c.TextChanged += new ChangeTextHandler(c_TextChanged);
} private void c_TextChanged(string s)
{
this.textBox1.Text = s;
}
}运行的时候"haha"怎么不能在textbox里显示?
(我一定要用事件)
子窗体中有一个TextBox.
Click主窗体上的一个Button1,打开子窗体.
然后子窗体的Form_Load事件中,把TextBox1.Text=str.我首先建了一个类cResult用来存储结果: public delegate void ChangeTextHandler(string s);
public class cResult
{
public event ChangeTextHandler TextChanged;
public void ChangeText(string s)
{
if (TextChanged != null)
{
TextChanged(s);
}
}
}
主窗体:
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
cResult c = new cResult();
c.ChangeText("haha!");
ChildForm cf = new ChildForm(c);
cf.ShowDialog();
}
}
子窗体:
public partial class ChildForm : Form
{
public ChildForm()
{
InitializeComponent();
}
private cResult c;
public ChildForm(cResult c)
: this()
{
this.c = c;
}
private void ChildForm_Load(object sender, EventArgs e)
{
c.TextChanged += new ChangeTextHandler(c_TextChanged);
} private void c_TextChanged(string s)
{
this.textBox1.Text = s;
}
}运行的时候"haha"怎么不能在textbox里显示?
(我一定要用事件)
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
ChildForm cf = new ChildForm(c);
cf.c.ChangeText("haha!"); cf.ShowDialog();
}
}