Say I have 2 Forms, Form1 and Form2.
Inside Form1, there is a button1, which when clicked goes to Form2.
Inside Form2, there is a button2, which when clicked goes to Form1.
Also, inside Form2, there is another button3, which when clicked perform some functions.
This button3 can ONLY be clicked ONCE, so I disable it after the button3 is being clicked.
The problem is when I go from Form2 to Form1 (hiding Form1), and when I want to come back
from Form1 to Form2, the button3 is disabled. How do I ensure that button3 is enabled when Form2 is freshly loaded the subsequent times? I also need the button3 to be able to clicked ONCE ONLY.
thanks.

1 answers
you can make a public static variable in other words a static bool isLoaded=false; in the Program.cs class. so when disabling the button the first time you can set the isLoaded to true by writing Program.isLoaded=true; and in the Form2_Load event handler check the Program.isLoaded value if its true or not in order to disable the button if its true. but this is done in case of the Form2 being created every time by new Form2();. It is better to create form1 and form2 instances when the program load and show/hide them instead of creating them everytime you want to switch, in this case you avoid loosing any changes when switching between forms. To do that make a new instance of Form1 and Form2 in the beginning of the main and control the form you want by for example
Form1 f1 = (Form1)Application.OpenForms["Form1"]; or
Form2 f2 = (Form2Application.OpenForms["Form2"];.
answered 2 years ago by:
1556
494
thanks muster for your nice suggestions, it works good. :)