我是想调A.close();但是,我无法从B类的方法中获得A的对象:(
高手如果有已经实现了的麻烦帖个完整的代码,先谢过啦!

解决方案 »

  1.   

    不好意思,我还是没有试出来:(
    我把我大概的想法说一下:
    A是程序的主窗体(用默认的方法自动生成的),包含按钮a,
    B是子窗体,包含按钮b,
    需要点击a显示B,再点击b关掉主窗体A,我在B的构造函数里用A类的对象做参数,但是还是不能在b的点击事件中找到
    A的实例,报错说Null Reference,
    我怎样才能使用带有main()函数的那个类呀?
      

  2.   

    A关了B也得完玩。方法一
    using System;
    using System.Windows.Forms;
    using System.IO;
    class Form1 : Form
    {
    private Button btn;
    public static Form FHandle;
    public Form1()
    {
    initCompont();
    }
    private void initCompont()
    {
    FHandle = this;
    this.Text = "Form1";
    btn = new Button();
    btn.Text = "Open Form2";
    btn.Click += new EventHandler(this.btn_Click);
    this.Controls.Add(btn);
    }
    void btn_Click(object sender, EventArgs e)
    {
    Form2 form2 = new Form2();
    //form2.Parent = this;
    form2.Show();
    }
    static void Main()
    {
    Application.Run(new Form1());
    }
    }
    class Form2 : Form
    {
    private Button btn;
    public Form2()
    {
    initCompont();
    }
    private void initCompont()
    {
    this.Text = "Form2";
    btn = new Button();
    btn.Text = "Close Form1";
    btn.Click += new EventHandler(this.btn_Click);
    this.Controls.Add(btn);
    }
    void btn_Click(object sender, EventArgs e)
    {
    Form1.FHandle.Close();
    }
    }方法二
    using System;
    using System.Windows.Forms;
    using System.IO;
    class Form1 : Form
    {
    private Button btn;
    public Form1()
    {
    initCompont();
    }
    private void initCompont()
    {
    this.Text = "Form1";
    btn = new Button();
    btn.Text = "Open Form2";
    btn.Click += new EventHandler(this.btn_Click);
    this.Controls.Add(btn);
    }
    void btn_Click(object sender, EventArgs e)
    {
    Form2 form2 = new Form2();
    form2.Opener = this;
    form2.Show();
    }
    static void Main()
    {
    Application.Run(new Form1());
    }
    }
    class Form2 : Form
    {
    private Button btn;
    public Form Opener;
    public Form2()
    {
    initCompont();
    }
    private void initCompont()
    {
    this.Text = "Form2";
    btn = new Button();
    btn.Text = "Close";
    btn.Click += new EventHandler(this.btn_Click);
    this.Controls.Add(btn);
    }
    void btn_Click(object sender, EventArgs e)
    {
    this.Opener.Close();
    }
    }
      

  3.   

    多谢,能留个email吗?以后好请你喝酒:)