在Form1 点击btn1按钮 打开Form2,同时传递一个参数,然后对Form2操作完后,关掉form2,同时返回一个参数,Form1接收后继续处理。

解决方案 »

  1.   

    在Form2中定义一个Delegate,在Form2_Close时FireEvents
    在Form1中绑定Form2定义的Delegate就可以实现详细可以参见MSDN中关于Delegate的例子:
    <SDK>v1.1\Samples\Technologies\DelegatesAndEvents
      

  2.   

    Form1的button事件中。
    Form2 f2=new Form2();
    f2.canshu=this.XXX;//想传啥过去在Form2中写个属性之类的就可以了
    DialogResult result =f2.ShowDialog();
    if (result == DialogResult.Cancel)
    {
    你要做的操作...
    f2.dispose();
    }
    在Form2 中Close按钮里面写上 
    this.DialogResult = DialogResult.Cancel;
      

  3.   

    用消息模式显示窗体DialogResult result =f2.ShowDialog();,就可以实现消息的返回啊!
      

  4.   

    新建两个窗口
    Form1.cs
    ------------------------------------------------------------------
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace Delegate
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    #region Definiens
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    #endregion #region Constructor
    public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }
    #endregion #region Dispose
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #endregion #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(72, 32);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(112, 32);
    this.button1.TabIndex = 0;
    this.button1.Text = "Show Form2";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 frm = new Form2();
    frm.OnSelected+=new Delegate.Form2.OnSelectedHandle(frm_OnSelected);
    frm.Show();
    } private void frm_OnSelected(string sInput)
    {
    MessageBox.Show("Return Value:"+sInput);
    }
    }
    }Form2.cs
    -----------------------------------------------------------------------------
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace Delegate
    {
    /// <summary>
    /// Summary description for Form2.
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    #region Delegate
    #region OnSelected
    /// <summary>
    /// 自定义动作的句柄,参数可以自定义
    /// 我现在用的string,也可以用别的参数类型
    /// </summary>
    public delegate void OnSelectedHandle(string sInput);
    public event OnSelectedHandle OnSelected;
    /// <summary>
    /// 触发OnSelected动作
    /// </summary>
    protected void FireOnSelected(string s)
    {
    if(OnSelected != null)
    OnSelected(s);
    }
    #endregion private System.Windows.Forms.TextBox textBox1;
    #endregion #region Definiens
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.Container components = null;
    #endregion #region Constructor
    public Form2()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    }
    #endregion #region Dispose
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
    #endregion #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(8, 8);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "test";
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.textBox1);
    this.Name = "Form2";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "Form2";
    this.Closed += new System.EventHandler(this.Form2_Closed);
    this.ResumeLayout(false); }
    #endregion private void Form2_Closed(object sender, System.EventArgs e)
    {
    //触发动作
    FireOnSelected(this.textBox1.Text);
    }
    }
    }
      

  5.   

    看这里,
    http://blog.csdn.net/zhzuo/archive/2004/04/05/22027.aspx