日期:2014-05-17  浏览次数:20857 次

c# winform委托事件的问题
两个窗体,第一个窗体上有一个文本框A,一个按钮A,点击按钮A,调用出第二个窗体,第二个窗体有个按钮B,点击按钮B调用一个类,类的一个返回字符串打印在文本框A中,怎么做。请教了

------解决方案--------------------
点击第1个窗体的按钮A,调出第2个窗体:
Form2 frm = new Form2(this.textBoxA);
frm.Show();

第2个窗体的构造函数里面:
TextBox txtBox;
public Form2(TextBox txt)
{
    InitializeComponent();
    txtBox = txt;
}
第2个窗体的按钮B的点击事件里:
txtBox.Text = 调用类的返回值;

------解决方案--------------------
Form1:代码

 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);
            }
        }