简体   繁体   中英

Close window C#

I have a window that has a button and the button is just supposed to create a window and then delete it. The variable online is initialized to false.

private void button2_Click(object sender, EventArgs e)
{
    ZeitenZeiger zeiten = new ZeitenZeiger();
    if (!online)
    {
        zeiten.Show();
        zeiten.ShowInTaskbar = false;
        online = true;
    }
    else
    {
        zeiten.Close();
        online = false;
    }
} 

Thanks for your help.

You create a new instance of zeiten every time the button is clicked.

So your .Close call calls another zeiten than your .Show opened.

Solve this by declaring zeiten on class level, and avoid closing it, instead hide it:

ZeitenZeiger zeiten = new ZeitenZeiger();
private void button2_Click(object sender, EventArgs e)
{
    if (!online)
    {
        zeiten.Show();
        zeiten.ShowInTaskbar = false;
    online = true;
    }
    else
    {
        zeiten.Hide();  // <-- Change close to hide!
        online = false;
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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