0

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.

1 Answer 1

2

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; } } 
Sign up to request clarification or add additional context in comments.

2 Comments

Note that after you have called .Close() for a form, you will no longer be able to call .Show(). I suspect that the OP wants to be able to show and hide the same form repeatedly, which will not be possible if it is closed.
You are right, he should use .Hide() instead of close - I change the code - thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.