Windows Develop Bookmark and Share   
 index > Windows Forms General > e.Cancel = true (disable application close alt-f4 & application.exit();) how to override?
 

e.Cancel = true (disable application close alt-f4 & application.exit();) how to override?

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

You can use google to search for other answers

Custom Search

More Threads

• SystemColor.InactiveCaptionText do not work with Vista
• What can i use here
• PasteHtml method (IHTMLTxtRange) doesnt take any effect on JScripts?
• Check for valid filename
• cannot open scheduled tasks on windows 2003 server
• Destroy objects with Dispose and Remove
• MouseMove event continually generated under Vista
• Draw lines over custom controls
• Align autosize labels on the right
• property grid questions