把一个值从Form1传到Form2,并且在点击Form1上的按钮之后,From2上的控件内容就被改成从Form1上传过来的值,该怎么做呢?
不知道该怎么保持Form1在Form2消失之后还是同一个对象
请教各位~

解决方案 »

  1.   

    假如Form2有个控件为文本输入控件:txtbox
    在Form2中,txtbox的内容取得为public在Form1中string stra;
    Form2 frm2 = new Form2();
    stra = frm2.txtbox.Text;当然是在同一个命名空间里
      

  2.   

    我想到了,但是不能立刻刷新Form1让更改后的文本显示出来,要怎么做呢?
      

  3.   

    可以考虑使用事件机制来完成,可以在Form2中定义一个自定义事件,并使用自定义的事件参数,在Form1中添加事件的处理代码,从事件参数里获取Form2中的数据并写到控件里,这样当Form2中的按钮点需的时候可以触发自定义事件,事件在Form1中被执行并写入Form1的控件里了。参考下面的思路:
    Form1中:
    private void button6_Click(object sender, EventArgs e)
    {
    Form2 frm = new Form2();
    frm.youEvent += new EventHandler<Form2.yourEventArgs>(frm_youEvent);
    frm.ShowDialog(this);
    this.listBox1.UpdateHScrllBar();
    }
    Form2中:
    public partial class Form2 : Form
    {
    public class yourEventArgs : EventArgs
    {
    private string m_value;
    public yourEventArgs(string value)
    {
    this.m_value = value;
    }
    public string Value
    {
    get { return m_value; }
    }
    }
    public event EventHandler<yourEventArgs> youEvent; public Form2()
    {
    InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
    if (this.youEvent != null)
    {
    yourEventArgs args = new yourEventArgs(this.textBox1.Text);
    this.youEvent(this, args);
    }
    }
    }
      

  4.   

    使用代理  事件实现两窗体实时交互见myblog:
    http://www.cnblogs.com/tuyile006/archive/2006/07/04/442113.html