Opening a form
frmConfiguration ConfigForm = new frmConfiguration(); ConfigForm.PassedValue1 = "This is a string I'm passing"; //TO OPEN AS A DIALOG FORM: ConfigForm.ShowDialog(); //or if (ConfigForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) else if (ConfigForm.DialogResult == System.Windows.Forms.DialogResult.Cancel) //TO OPEN NOT AS A DIALOG FORM: ConfigForm.Show();
Closing a form
Close(); //or this.Close();
Closing a form from the Form Load Event
You can't use this.Close as the form has not yet finished loading so if you do you'll cause fatal error. Instead use this:
this.BeginInvoke(new MethodInvoker(this, System.Windows.Forms.Form.Close));
Dynamically updating Form Properties
Use this instead of the forms name:
this.Text = "A New Title For This Form";
To Disable A Form
//This form this.Enabled = false; //Another form frmMyForm.Enabled = false;
Why Is A Form Closing?
You can use things like this (see the CloseReason members):
if (e.CloseReason == System.Windows.Forms.CloseReason.UserClosing)
Supressing Display Update Of A Form
You can't stop a form being painted, but you can do this:
this.SuspendLayout(); MyControlName1.SuspendLayout(); MyControlName2.SuspendLayout(); ... MyControlName1.ResumeLayout(); MyControlName2.ResumeLayout(); this.ResumeLayout();
You can also make specific controls invisible
Default Accept and Cancel Buttons
Set in the forms AcceptButton and CancelButton properties.
Set Dialog result for each button
Returning a Dialog Result
You can assign a dialog result value to buttons on the form in their properties.
Stopping A Form / Application From Closing
Handle the form's 'Closing' event and set 'e.Cancel' to 'True' if you want to prevent the form from closing.
e.Cancel = true;
If your going to present a message box, such as a 'do you want to save' then you need to do the cancel first to stop the ap closing while waiting for the user input. Then afterwards do the close.