3
Answers

Open/Close form on same button

Ask a question
Kim Larsen

Kim Larsen

15y
5.7k
1
Hey all! I have a problem, which consist of a button that is supposed to open a form, and if i click that button again, the Form should close, and only 1 of this form should be able to run at the time. Problem is that it keeps opening more forms as i click my button. [code] private void button1_Click(object sender, EventArgs e) { int on = 1; int off = 0; StartMenu StartMenu = new StartMenu(); if (on < off) { StartMenu.Dispose(); on = 1; off = 0; } else if(on > off) { StartMenu.Show(); off = 1; on = 0; } } [\code] This is the code that i attempted to make for the button to register whether the button is active or inactive, and choose if the Form should open or close. :) Hope you can help!! Looking into it my code was not readable, i'll try to organize it. :)

Answers (3)