问题是这样的:有3个窗体 一个主窗体form1, 还有form2和form3form2上有个textbox控件,form3上有个textbox控件和一个button控件;form3上的textbox.text="你好"; 点击form3上的button后使form2中的textbox内容等于form3中的textbox内容,应该怎么做,请各位大哥大姐指教,急切等待中.

解决方案 »

  1.   

    最简单的办法 Form _From = Application.OpenForms["Form2的Name"];            if (_From != null)
                {
                    Control _Control = _From.Controls["Form2的TextBox.Name"];
                    if (_Control != null) _Control.Text = "设置值";
                }
      

  2.   

    增加一个类,里面放置一个静态的textbox变量,指向form2的textbox
    form3的button点击后,直接修改静态变量的textbox.Text属性
      

  3.   


    public static Form f2 = new form2();//这个form2需要记录下来,这样后面的form3才能访问到。

    f2.TextBox1.Text = this.Textbox1.Text; //form3上执行这一句代码,f2上的Textbox1需要改成public属性,默认是private的,外部不能访问。
      

  4.   

    建立一个 Dictionary<string, Form>
    打开form2和form3的时候把窗体添加进Dictionary,并给唯一标识名称
    点中form3的BUTTON时先判断是否为空
    if (Dictionary["form2"] == null || Dictionary["form3"].IsDisposed)
    然后Dictionary["form2"].TextBox1.Text=Dictionary["form3"].TextBox1.TextTextBox1需要声明为Public
      

  5.   

    我刚学习C#
     能举个例子提示一下吗?相关的也可以>
      

  6.   

    if (Dictionary["form2"] == null || Dictionary["form3"].IsDisposed)
    ===》
    if (Dictionary["form2"] != null)
      

  7.   


    //form3中
     private void buttonOnClink(object sender, EventArgs e)
     {
         Form2 fr = new Form2();
         fr.textBox1.Text= this.textBox1.Text;
         fr.Show();
     }
      

  8.   

    wangsaokui
    建立一个 Dictionary <string, Form> 
    打开form2和form3的时候把窗体添加进Dictionary,并给唯一标识名称.这个该怎么做啊,刚学,不太懂,请多指教.我试了上面发的好多不行,这个问题在VB.net中很好实现,但是在C#中就不知道该怎么实现了
     帮忙啊
      

  9.   

    http://blog.caozhongyan.com/files/窗体通讯code.rar
    给分吧。
      

  10.   

    Dictionary <string, Form> testDict = new Dictionary() <string, Form>;
    .....
    form2 form2instance = new form2();
    testDict.Add("form2", form2instance );
    form3 form3instance = new form3();
    testDict.Add("form3", form3instance );
      

  11.   

    字典和委托都不是必需的。他们说的委托,是指让form2可以被通知到form3的改变自己去取值。建议你还是从基础开始学习。
      

  12.   

    挖撒
      caozhy大哥太有魅力了
     真是太感谢了,thank you
     揭贴了 谢谢大家的支持.有大家的支持才有我发贴的动力. 
      

  13.   

    如果不只一个form,需要同时改变的时候这个就比较有用了。
    用委托的方式是说一个窗体的控件内容改变后(当然也可以是WebService接收到数据,或者SOCKET底层接收到数据,或者数据库访问层定时更新等等多种情况),抛出一个事件,
    而先前需要做一个委托,接收到这个数据改变的事件后去改变其它窗体控件的内容(一般在窗体上写一个函数,接收数据并修改控件的值,委托去调用该窗体函数并传递数据)。
      

  14.   

    看得出来wangsaokui是大牛,不过把问题考虑复杂了。再深入下去,连thread pool都要出来了。-_-#
      

  15.   

    To: caozhy
    呵呵,今天开心,第5次连任MVP,上来逛逛!
      

  16.   

    帖子揭的有点快了.这里我写了个委托的例子.
    FORM1.CS
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication8
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.Show();
                Form3 frm3 = new Form3(frm);
                frm3.Show();
            }
        }
    }
    FORM2.CS
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication8
    {
        public delegate void calleventhandler(string strMsg);
        public partial class Form2 : Form
        {
            public event calleventhandler eventmsg;
            public Form2()
            {
                InitializeComponent();
            }
            public void Run(string strMsg)
            {
                if (eventmsg != null)
                {
                    eventmsg(strMsg);
                }
            }
            public void RecvMsg(string strMsg)
            {
                this.textBox2.Text = strMsg;
            }
        }
    }
    FORM3.CS
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication8
    {
        public partial class Form3 : Form
        {
            Form2 frm;
            public Form3(Form2 frm)
            {
                InitializeComponent();
                this.frm = frm;
            }        private void button1_Click(object sender, EventArgs e)
            {
                frm.eventmsg += new calleventhandler(frm.RecvMsg);
                frm.Run(this.textBox1.Text);
            }
        }
    }