473,440 Members | 5,215 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

How do I intercept a close event?

How do I change a window's behavior so when someone clicks the little 'X' in
the upper right corner it actually hides the dialog, instead of disposing it?
Nov 16 '05 #1
8 21565
Hi,

In Closing event set e.Cancel to true and WindowState to
FormWindowState.Minimized.

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"MrNobody" <Mr******@discussions.microsoft.com> je napisal v sporočilo
news:73**********************************@microsof t.com ...
How do I change a window's behavior so when someone clicks the little 'X'
in
the upper right corner it actually hides the dialog, instead of disposing
it?

Nov 16 '05 #2
One note on that though. You should first check if the computer is
being shut down, otherwise the form will not automatically be closed
and the computer will not turn off.
private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if( !Environment.HasShutdownStarted )
{
e.Cancel = true;
this.Hide();
}
}

Nov 16 '05 #3
I cannot get Environment.HasShutdownStarted to work.
I placed it in;
private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

but it always returns false, even when shutting down WindowsXP prof.

I placed this MessageBox in the same routine;
MessageBox.Show(Environment.HasShutdownStarted.ToS tring(),"Shutting Down?");
which always returns FALSE.

I'm at a loss with how to differentiate between the User closing the app
down and Windows closing the app down

I even wrote a second very simple app and placed the
Environment.HasShutdownStarted in the form1_closing event. Still always
returns false.

I must be missing something obvious?

Tinius
"Greg Bacchus" wrote:
One note on that though. You should first check if the computer is
being shut down, otherwise the form will not automatically be closed
and the computer will not turn off.
private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if( !Environment.HasShutdownStarted )
{
e.Cancel = true;
this.Hide();
}
}

Nov 16 '05 #4
Hi Tinius,

I think that the easiest way of capturing the close event of the form, is
in fact to capture the WM_CLOSE message that's being send to the form through
the message loop of the OS. Here's an easy way for doing that

//insert the following code inside your main form class:

const int WM_CLOSE = 16;

protected override void WndProc(ref Message m)
{
base.WndProc (ref m);

if ( WM_CLOSE == m.Msg )
{
//closing
Trace.WriteLine(Application.ExecutablePath + " is exiting !!!");
}
}

Cheers,
Branimir

Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org
I cannot get Environment.HasShutdownStarted to work.
I placed it in;
private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
but it always returns false, even when shutting down WindowsXP prof.

I placed this MessageBox in the same routine;
MessageBox.Show(Environment.HasShutdownStarted.ToS tring(),"Shutting
Down?"); which always returns FALSE.

I'm at a loss with how to differentiate between the User closing the
app down and Windows closing the app down

I even wrote a second very simple app and placed the
Environment.HasShutdownStarted in the form1_closing event. Still
always returns false.

I must be missing something obvious?

Tinius

"Greg Bacchus" wrote:
One note on that though. You should first check if the computer is
being shut down, otherwise the form will not automatically be closed
and the computer will not turn off.

private void MainForm_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if( !Environment.HasShutdownStarted )
{
e.Cancel = true;
this.Hide();
}
}

Nov 16 '05 #5
Hi Branimir and thanks for the suggestion

I tried what you suggested and its works fine for detecting when the User
closes the app. The WM_CLOSE is received directly after MainForm_Closing gets
executed.
I still am in the situation where I cannot find a way of determining when
the App is closed via Shutdown.

Tinius

"Branimir Giurov [C# MVP]" wrote:
Hi Tinius,

I think that the easiest way of capturing the close event of the form, is
in fact to capture the WM_CLOSE message that's being send to the form through
the message loop of the OS. Here's an easy way for doing that

//insert the following code inside your main form class:

const int WM_CLOSE = 16;

protected override void WndProc(ref Message m)
{
base.WndProc (ref m);

if ( WM_CLOSE == m.Msg )
{
//closing
Trace.WriteLine(Application.ExecutablePath + " is exiting !!!");
}
}

Cheers,
Branimir

Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org


Nov 16 '05 #6
How about WM_QUERYENDSESSION?

"Tinius" <Ti****@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Hi Branimir and thanks for the suggestion

I tried what you suggested and its works fine for detecting when the User
closes the app. The WM_CLOSE is received directly after MainForm_Closing
gets
executed.
I still am in the situation where I cannot find a way of determining when
the App is closed via Shutdown.

Tinius

"Branimir Giurov [C# MVP]" wrote:
Hi Tinius,

I think that the easiest way of capturing the close event of the form, is
in fact to capture the WM_CLOSE message that's being send to the form
through
the message loop of the OS. Here's an easy way for doing that

//insert the following code inside your main form class:

const int WM_CLOSE = 16;

protected override void WndProc(ref Message m)
{
base.WndProc (ref m);

if ( WM_CLOSE == m.Msg )
{
//closing
Trace.WriteLine(Application.ExecutablePath + " is exiting !!!");
}
}

Cheers,
Branimir

Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org

Nov 16 '05 #7
Good call :)

Cheers,
Branimir

Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org
How about WM_QUERYENDSESSION?

"Tinius" <Ti****@discussions.microsoft.com> wrote in message
news:F4**********************************@microsof t.com...
Hi Branimir and thanks for the suggestion

I tried what you suggested and its works fine for detecting when the
User
closes the app. The WM_CLOSE is received directly after
MainForm_Closing
gets
executed.
I still am in the situation where I cannot find a way of determining
when
the App is closed via Shutdown.
Tinius

"Branimir Giurov [C# MVP]" wrote:
Hi Tinius,

I think that the easiest way of capturing the close event of the
form, is
in fact to capture the WM_CLOSE message that's being send to the
form
through
the message loop of the OS. Here's an easy way for doing that
//insert the following code inside your main form class:

const int WM_CLOSE = 16;

protected override void WndProc(ref Message m)
{
base.WndProc (ref m);
if ( WM_CLOSE == m.Msg )
{
//closing
Trace.WriteLine(Application.ExecutablePath + " is exiting !!!");
}
}
Cheers,
Branimir
Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org

Nov 16 '05 #8
Hi Sean and Branimir

Thanks, it works now exactly as I wanted it to.

But I'm surprised by the C# environment lacking a high level command to deal
with such a basic issue.

I previously had code in the MainForm_Closing event as I was using e.cancel
to prevent actual close down. I moved this code to the WM_CLOSE intercept,
and didn't pass on the WM_CLOSE back to the system, thus reproducing e.cancel.

I used the WM_QUERYENDSESSION to detect Windows Shutdown to do some final
tidying up.

I come from a VB background which has can detect the following 5 ways that
may be causing an app to close down.
vbFormControlMenu 0 The user chose the Close command from the Control menu
on the form.
vbFormCode 1 The Unload statement is invoked from code.
vbAppWindows 2 The current Microsoft Windows operating environment session
is ending.
vbAppTaskManager 3 The Microsoft Windows Task Manager is closing the
application.
vbFormMDIForm 4 An MDI child form is closing because the MDI form is
closing.
vbFormOwner 5 A form is closing because its owner is closing.

I see now that I can do all this by intercepting the appropriate messages
but hope that in the next C# upgrade, we will be given the high level
funtions to perform these.

Thanks once again

Tinius

"Branimir Giurov [C# MVP]" wrote:
Good call :)

Cheers,
Branimir

Branimir Giurov
C# MVP, MCSD, MCDBA, MCT
CTO, BSH Ltd.
www.sofiadev.org
How about WM_QUERYENDSESSION?


Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Kai Grossjohann | last post by:
It seems that Ctrl-N in Mozilla opens a new empty browser window. That's fine, I don't need to do anything about it. But Ctrl-N in IE appears to clone the current window. Is there a way to...
1
by: Morten Overgaard | last post by:
Hi all Is it possible for a .Net app. to intercept the "open" action on files in a given directory. I want to intercept the "open" action and the perhaps direct the client ( the app. which is...
1
by: zoltix | last post by:
Hi, I am beginner in JavaScript. I would like to intercept all click events on my document. I use this function for that document.onmousedown=click;. It works well. But I would like to...
0
by: zoltix | last post by:
Hi, I am beginner in aspx. I would like to intercept all click events on my document. I use this function in javascipt for that document.onmousedown=click;. It works well. But I would...
25
by: moondaddy | last post by:
I have an application where users need to upload images and in my web.config file I have a setting like this: <httpRuntime maxRequestLength="512" /> Which restricts image larger than 500k from...
1
by: Daniele | last post by:
I have to start my public sub chiudi() when i close my page with the 'X' (tall right) please help me and send me little exemple tanks Daniele
3
by: lookaround | last post by:
Hi everyone, I need some help... I call an external exe (a command-line tool) with Process.Start through this code: try { p = new Process(); p.StartInfo.UseShellExecute = false;...
7
by: Lit | last post by:
Hi, I have an update panel with a Submit Button. ( AJAXified ) Is there a way, good way, of Intercepting a submit before it happens? I would like to do something in JavaScript before...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.