参考《C#Windows程序设计》
里面有一章专门讲dialog

解决方案 »

  1.   

    在main()所在的类中声明一个变量public型的,在关闭f2时将需要传递的值赋给此变量
      

  2.   

    Form1:...
    private string _receivedStr;
    public string ReceivedString
    {
       get{
          return _receivedStr;
       }
       set {
          _receivedStr = value;
       }
    }private void ShowForm2()
    {
        Form2 frm = new Form2();
        frm.Show( this );
    }
    ...Form2 OnClosed:  ((Form1)this.Parent).ReceivedString = "Some value";
      

  3.   

    不好意思
    Show Form2的时候应该是:private void ShowForm2()
    {
        Form2 frm = new Form2();
        frm.ShowDialog( this );
    }
      

  4.   

    可以不用全局变量,你不是自己说的再form1中申明一个form2的对象,我觉得这就已经足够了。实际上还是全局变量,自是范围不是class级别的。
      

  5.   

    将数据声明为static变量也可以
      

  6.   

    大伙很多的意思都是要用 showDialog()这个方法,可是这样的话,Form2就成了模态对话框,而我的意思是说,Form2是非模态对话框,他们可以同时运行的那一种.不是非要等form2关闭之后才能获得数据,所以,还请高手再指点一下.
      

  7.   

    修改form2的构造函数:public Form2( Form1 frm )
    {
       this._form1 = frm;
    }private Form1 _form1;OnClosed:
    if( this._form1!=null )
      this._form1.ReceivedString = "Some Value";Form1中的代码:
    ...
    private string _receivedStr;
    public string ReceivedString
    {
       get{
          return _receivedStr;
       }
       set {
          _receivedStr = value;
       }
    }private void ShowForm2()
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }
    ...
      

  8.   

    我按照大家说的,把form1中接收数据的label1设为public,form2中要传递的数据为textBox1的值,然后在form2中声明form2_cloing函数
    利用
    ((Form1)this.Parent).label1.Text = this.textBox1.Text;传递数据,编译时没有错误,可是在运行关闭Form2时,出现错误.详细如下.
    有关调用实时 (JIT) 调试而不是此对话框的详细信息,
    请参阅此消息的结尾。************** 异常文本 **************
    System.NullReferenceException: 未将对象引用设置到对象的实例。
       at test1.Form2.Form2_Closing(Object sender, CancelEventArgs e) in f:\projects\test1\form2.cs:line 96
       at System.Windows.Forms.Form.OnClosing(CancelEventArgs e)
       at System.Windows.Forms.Form.WmClose(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    ************** 已加载的程序集 **************
    mscorlib
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3705.209
        基本代码:file:///c:/windows/microsoft.net/framework/v1.0.3705/mscorlib.dll
    ----------------------------------------
    test1
        程序集版本:1.0.1170.32516
        Win32 版本:1.0.1170.32516
        基本代码:file:///F:/projects/test1/bin/Debug/test1.exe
    ----------------------------------------
    System.Windows.Forms
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3705.0
        基本代码:file:///c:/windows/assembly/gac/system.windows.forms/1.0.3300.0__b77a5c561934e089/system.windows.forms.dll
    ----------------------------------------
    System
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3705.0
        基本代码:file:///c:/windows/assembly/gac/system/1.0.3300.0__b77a5c561934e089/system.dll
    ----------------------------------------
    System.Drawing
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3705.0
        基本代码:file:///c:/windows/assembly/gac/system.drawing/1.0.3300.0__b03f5f7f11d50a3a/system.drawing.dll
    ----------------------------------------
    System.Xml
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3705.0
        基本代码:file:///c:/windows/assembly/gac/system.xml/1.0.3300.0__b77a5c561934e089/system.xml.dll
    ----------------------------------------
    mscorlib.resources
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3300.0
        基本代码:file:///c:/windows/assembly/gac/mscorlib.resources/1.0.3300.0_zh-chs_b77a5c561934e089/mscorlib.resources.dll
    ----------------------------------------
    System.Windows.Forms.resources
        程序集版本:1.0.3300.0
        Win32 版本:1.0.3300.0
        基本代码:file:///c:/windows/assembly/gac/system.windows.forms.resources/1.0.3300.0_zh-chs_b77a5c561934e089/system.windows.forms.resources.dll
    ----------------------------------------************** JIT 调试 **************
    若要启用实时 (JIT) 调试,此
    应用程序或计算机的配置文件 (machine.config) 的 
     system.windows.forms 节中必须设置 jitDebugging 值。
    编译应用程序时还必须启用
    调试。例如:<configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>启用 JIT 调试后,任何未处理的异常
    将被发送到此计算机上注册的 JIT 调试器,
    而不是由此对话框处理。
      

  9.   

    //Form1中
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 fm = new Form2();
    fm.Show();
    }

    public static  void ShowResult()
    {
    MessageBox.Show(Class1.getValue());

    }//Form2中
    private void button1_Click(object sender, System.EventArgs e)
    {
    Class1.setValue(this.textBox1.Text);
    this.Visible = false;

    Form1.ShowResult();
    this.Close();


    }
    //Class1中
    public class Class1
    {
    static string t;
    public Class1()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    } public static void setValue(string sNew)
    {
    t = sNew;
    } public static string getValue()
    {
    return t;
    }
    }
    ============================================================
    Class1即可在Form1中访问,也可在Form2中访问
      

  10.   

    这个问题直接用showdialog()方法就行了
    然后判断是点了那个按钮,再直接调用form2上的控件就可以了
      

  11.   

    gujianxin(木头象),大哥,还是你的经典啊!佩服佩服~!!