我在form1 里点击button1 ,然后是form2.show,我想在关掉form2的同时也将form1也关掉,应该怎么做,谢谢,急

解决方案 »

  1.   

    不好意思这里是C#区。
    应该是this.Hide;
      

  2.   

    可以用事件委托
    给你个例子
    主窗体代码:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    namespace 委托传消息
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.TextBox textBox1;
    private Form2 myForm = new Form2 ( ) ; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    this.myForm.Send += new Form2.SendMess( Send );
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button1.Location = new System.Drawing.Point(80, 72);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(64, 64);
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(128, 40);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "textBox1";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Send ( string str )
    {
    textBox1.Text = str ;
    MessageBox.Show("textBox1.Text = " + textBox1.Text);
    //把接收来的字符串通过TextBox组件显示出来
    //this.button2.PerformClick();
    } private void button1_Click(object sender, System.EventArgs e)
    {
    myForm.ShowDialog ( ) ;
    //显示从窗体
    }
    }
    }从窗体代码
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;namespace 委托传消息
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button button1;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form2()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.label1 = new System.Windows.Forms.Label();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(30, 62);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(163, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "传递到主窗体的字符串:";
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(37, 94);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(187, 21);
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "";
    // 
    // button1
    // 
    this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.button1.Location = new System.Drawing.Point(99, 155);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(85, 41);
    this.button1.TabIndex = 2;
    this.button1.Text = "传递";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.label1);
    this.MaximizeBox = false;
    this.MinimizeBox = false;
    this.Name = "Form2";
    this.Text = "Form2";
    this.ResumeLayout(false); }
    #endregion public delegate void SendMess( string str );
    //定义委托类型
    public event SendMess Send;
    //定义一个事件类型 private void button1_Click(object sender, System.EventArgs e)
    {
    //MessageBox.Show( textBox1.Text );
    Send( textBox1.Text ); }
    }
    }
      

  3.   

    不要用委托这么烦吧。只要把form1传给form2,在form2里面调用form1的close或者hide就可以了
    class Form1 : ...
    {
       private void btn1_click(...)
      {
        Form2 form = new Form2(this);
        form.ShowDialog();
      }
    }class Form2 : ...
    {
       private Form1 theForm = null;
       public Form2(Form1 theForm)
       {
          this.theForm = theForm;
       }
       private void btn2_click(...)
       {
          theForm.Close();
          Close();
        } 
    }