简体   繁体   English

单击按钮时关闭表格

[英]Close form when clicking on button

I was here testing a situation in which the main form (Form1) when initiating also opens Form3 and when clicking on a button opens Form2 and closes Form3. 我在这里测试一种情况,在这种情况下,启动时主窗体(Form1)也会打开Form3,而单击按钮时会打开Form2并关闭Form3。 But I'm getting Form3 continuously open, no close when clicking on the button. 但是我正在不断打开Form3,单击该按钮时没有关闭。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TESTE2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Form test = new Form3();
            test.Show();
        }

        private Form3 form3;

        private void button1_Click(object sender, EventArgs e)
        {
            Form test = new Form3();

            test.Close();
            Form test2 = new Form2();
            test2.Show();
            this.Hide();
        }
    }
}

what am I doing wrong? 我究竟做错了什么?

Thanks, 谢谢,

You are using a different reference to open the form and to close it. 您正在使用其他引用打开并关闭该窗体。 Here's a revised version of your code: 这是您代码的修订版:

public partial class Form1 : Form
{
    private Form3 form3;

    public Form1()
    {
        InitializeComponent();
        form3 = new Form3();
        form3.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        form3.Close();
        Form test2 = new Form2();
        test2.Show();
        this.Hide();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM