Hello i have a question:
Prevent form from closing:
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; }
Button to close form:
private void button1_Click(object sender, EventArgs e) { Application.Exit(); }
But how do i set the e.Cancel = false; from the button1?
thanks!
|
| OMEGA_ReD Tuesday, April 22, 2008 3:52 PM |
You don't, use a variable alongside to decide wether to allow closing or not:
private Boolean _AllowClose = false;
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { if (!_AllowClose) e.Cancel = true; }
private void button1_Click(object sender, EventArgs e) { _AllowClose = true; Close(); }
|
| Lasse V. Karlsen Tuesday, April 22, 2008 3:55 PM |
i found a solution: declaire a global variable.
Class: GlobalVar.cs
static class GlobalClass { private static bool m_globalVar = false;
public static bool AllowClose { get { return m_globalVar; } set { m_globalVar = value; } }
under close button: private void btnCloseApp_Click(object sender, EventArgs e) { GlobalClass.AllowClose = true; Application.Exit(); }
Form closing event:
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { if (!GlobalClass.AllowClose) { e.Cancel = true; } }
|
| OMEGA_ReD Tuesday, April 22, 2008 9:04 PM |
You don't, use a variable alongside to decide wether to allow closing or not:
private Boolean _AllowClose = false;
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { if (!_AllowClose) e.Cancel = true; }
private void button1_Click(object sender, EventArgs e) { _AllowClose = true; Close(); }
|
| Lasse V. Karlsen Tuesday, April 22, 2008 3:55 PM |
it works!
Thanks!!
|
| OMEGA_ReD Tuesday, April 22, 2008 4:02 PM |
| Lasse Vågsæther Karlsen wrote: |
You don't, use a variable alongside to decide wether to allow closing or not:
private Boolean _AllowClose = false;
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { if (!_AllowClose) e.Cancel = true; }
private void button1_Click(object sender, EventArgs e) { _AllowClose = true; Close(); }
| | is it possible to mod the code so i can close the application from another form? thanks! |
| OMEGA_ReD Tuesday, April 22, 2008 4:30 PM |
any1? |
| OMEGA_ReD Tuesday, April 22, 2008 7:13 PM |
i found a solution: declaire a global variable.
Class: GlobalVar.cs
static class GlobalClass { private static bool m_globalVar = false;
public static bool AllowClose { get { return m_globalVar; } set { m_globalVar = value; } }
under close button: private void btnCloseApp_Click(object sender, EventArgs e) { GlobalClass.AllowClose = true; Application.Exit(); }
Form closing event:
private void frmLogin_FormClosing(object sender, FormClosingEventArgs e) { if (!GlobalClass.AllowClose) { e.Cancel = true; } }
|
| OMEGA_ReD Tuesday, April 22, 2008 9:04 PM |