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

解决方案 »

  1.   

    点击第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 = 调用类的返回值;
      

  2.   


    调用类的返回值,实际上在类里面执行的是个线程,在线程里面打日志出来,所以要这样用,后面线程的时候就说是 非创建线程 不能更改
    一下是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);
            }
    随意写的,没有太规范,再请教一下,谢谢
      

  3.   

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