我想实现如下功能:父窗体生成一个子窗体,在自窗体内的文本框内输入一个值,点击确定,自窗体关闭,并将文本框内的值传给父窗体,父窗体利用这个值改变自身某个控件的内容。
我再网上搜了好久还没找到答案。
哪位前辈能教下小弟,小弟感激不敬。

解决方案 »

  1.   

    又是这样的问题。。
    首先 
    form1.cs文件中又一个button事件和一个方法。
    private void button5_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();//第一种方法,把form2的setText事件交给form1的GetText(string text)方法。
                f2.T += new Form2.SetText(GetText);
                f2.ShowDialog();//第2中方法把form1的对象传给form2
              Form2 ff = new Form2(this);
            }        public void GetText(string text)
            {
                string value = text;
            }然后就是
    form2.cs文件
     public partial class Form2 : Form
        {
    //第一种方法就是代理。
            public delegate void SetText(string text);
            public event SetText T;        Form1 f1 = null;        public Form2()
            {
                InitializeComponent();
            }//第2中方法
            public Form2(Form1 parentForm)
            {
                this.f1 = parentForm;
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
    //第2中方法,这里可以调用form1的公开方法和属性,随便你喜欢那一种都可以
                f1.GetText(textBox1.Text);//第一种方法
                if (T != null)
                    T(textBox1.Text);            this.Close();
            }
        }
      

  2.   

    父窗体
    Form2 ff = new Form2();
                if (ff.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show(ff.s);
                }子窗体
     public string s;
            private void button1_Click(object sender, EventArgs e)
            {
                s = this.textBox1.Text;
                this.DialogResult = DialogResult.OK;
            }
      

  3.   

    添加一个用户类,例如uc.cs,这里面放一个public static string strVar;
    子窗体按钮事件:uc.strVar=textBox1.Text;
    子窗体关闭以后,父窗体可以接收到该变量的值,也使用uc.strVar。
      

  4.   

    winform下可以直接调用父窗体变量.方法或控件,或楼上的一些方法来实现
    asp.net下可刷新父窗体,并传值过去来实现
      

  5.   

    我也遇到你一样的问题过1 楼是个好办法,线程安全的还有中就是mainform中
    public void updataControl(string txt)
    {
         TextBox1.Text=txt;
    }private void button1_Click(object sender, EventArgs e) 
    {
         SubForm frmSub= new SubForm ();
    //SubForm frmSub= new SubForm (this);//构造函数传对象     frmSub.frmMain= this;
          frmSub.show();
    }在SubForm public MainForm frmMain;private void buttonOK_Click(object sender, EventArgs e) 
    {
              frmMain.updataControl (TextBox1.Text) ;
       this.Close()
    }等等类似
      

  6.   

    我用的5楼的方法来向父窗口回填新建的DataGrid值,太感谢了!这点东西折腾了一下午,总算在这找到答案了!