Form and "X" button

  • Thread starter Thread starter Marty
  • Start date Start date
M

Marty

Hi,

How can I detect that a VS.NET2003 C# form is closed with the upper
right "X" button ?

This is because I want to add code to the myForm.OnClosing() event to do
a different process if a form is closed by the user (with the "X"
button) or if the form is closed with myForm.close().

Thank you!
Marty
 
Set a global boolean variable (field), and write a method that does the
myForm.Close() work. The method would set the variable to indicate that the
method is closing the form. The OnClosing() mathod can then check that
variable (field) to find out whether the form was closed by your method or
not.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
Marty,

When you make a call to Close, why not set a flag in your form which
indicates that it is you who made the call. Then, in the on closing event,
you can check the flag. If it is set, you closed the form, otherwise, the
user did it, and you can perform whatever action you need.

Hope this helps.
 
Here is a sample to check if X button is clicked...

public const int SC_CLOSE = 61536;

public const int WM_SYSCOMMAND = 274;

public bool _closeClick = false;

protected override void WndProc(ref Message m)

{

if ( m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE )

{

this._closeClick = true;

}

base.WndProc (ref m);

}

private Form1_Closing(object sender, System.ComponentModel.CancelEventArgs
e)

{

if ( this._closeClick )

{

// X button clicked..

}

}

Sorry for the bad format.. my notepad is screwed up... and I had to paste it
directly from VS.NET code....

VJ

Nicholas Paldino said:
Marty,

When you make a call to Close, why not set a flag in your form which
indicates that it is you who made the call. Then, in the on closing
event, you can check the flag. If it is set, you closed the form,
otherwise, the user did it, and you can perform whatever action you need.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marty said:
Hi,

How can I detect that a VS.NET2003 C# form is closed with the upper right
"X" button ?

This is because I want to add code to the myForm.OnClosing() event to do
a different process if a form is closed by the user (with the "X" button)
or if the form is closed with myForm.close().

Thank you!
Marty
 
Hi VJ,

Thank you for your help :) Where can I find the definitions of constants
like WM_SYSCOMMAND and SC_CLOSE, maybe there are other const that I
could use for future process, I find them useful.

Thanks,
Marty
Here is a sample to check if X button is clicked...

public const int SC_CLOSE = 61536;

public const int WM_SYSCOMMAND = 274;

public bool _closeClick = false;

protected override void WndProc(ref Message m)

{

if ( m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE )

{

this._closeClick = true;

}

base.WndProc (ref m);

}

private Form1_Closing(object sender, System.ComponentModel.CancelEventArgs
e)

{

if ( this._closeClick )

{

// X button clicked..

}

}

Sorry for the bad format.. my notepad is screwed up... and I had to paste it
directly from VS.NET code....

VJ

Marty,

When you make a call to Close, why not set a flag in your form which
indicates that it is you who made the call. Then, in the on closing
event, you can check the flag. If it is set, you closed the form,
otherwise, the user did it, and you can perform whatever action you need.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

How can I detect that a VS.NET2003 C# form is closed with the upper right
"X" button ?

This is because I want to add code to the myForm.OnClosing() event to do
a different process if a form is closed by the user (with the "X" button)
or if the form is closed with myForm.close().

Thank you!
Marty
 
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinUser.h assuming you have VS.NET
Enterprise or VC++.

Jason Newell

Hi VJ,

Thank you for your help :) Where can I find the definitions of constants
like WM_SYSCOMMAND and SC_CLOSE, maybe there are other const that I
could use for future process, I find them useful.

Thanks,
Marty
Here is a sample to check if X button is clicked...

public const int SC_CLOSE = 61536;

public const int WM_SYSCOMMAND = 274;

public bool _closeClick = false;

protected override void WndProc(ref Message m)

{

if ( m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE )

{

this._closeClick = true;

}

base.WndProc (ref m);

}

private Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

if ( this._closeClick )

{

// X button clicked..

}

}

Sorry for the bad format.. my notepad is screwed up... and I had to
paste it directly from VS.NET code....

VJ

Marty,

When you make a call to Close, why not set a flag in your form
which indicates that it is you who made the call. Then, in the on
closing event, you can check the flag. If it is set, you closed the
form, otherwise, the user did it, and you can perform whatever action
you need.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi,

How can I detect that a VS.NET2003 C# form is closed with the upper
right "X" button ?

This is because I want to add code to the myForm.OnClosing() event
to do a different process if a form is closed by the user (with the
"X" button) or if the form is closed with myForm.close().

Thank you!
Marty
 
Thank you :)
Marty

Jason said:
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinUser.h assuming you have VS.NET
Enterprise or VC++.

Jason Newell

Hi VJ,

Thank you for your help :) Where can I find the definitions of
constants like WM_SYSCOMMAND and SC_CLOSE, maybe there are other const
that I could use for future process, I find them useful.

Thanks,
Marty
Here is a sample to check if X button is clicked...

public const int SC_CLOSE = 61536;

public const int WM_SYSCOMMAND = 274;

public bool _closeClick = false;

protected override void WndProc(ref Message m)

{

if ( m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE )

{

this._closeClick = true;

}

base.WndProc (ref m);

}

private Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

if ( this._closeClick )

{

// X button clicked..

}

}

Sorry for the bad format.. my notepad is screwed up... and I had to
paste it directly from VS.NET code....

VJ

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message
Marty,

When you make a call to Close, why not set a flag in your form
which indicates that it is you who made the call. Then, in the on
closing event, you can check the flag. If it is set, you closed the
form, otherwise, the user did it, and you can perform whatever
action you need.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi,

How can I detect that a VS.NET2003 C# form is closed with the upper
right "X" button ?

This is because I want to add code to the myForm.OnClosing() event
to do a different process if a form is closed by the user (with the
"X" button) or if the form is closed with myForm.close().

Thank you!
Marty
 
Thanks Marty...

Marty said:
Thank you :)
Marty

Jason said:
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\PlatformSDK\Include\WinUser.h assuming you have VS.NET
Enterprise or VC++.

Jason Newell

Hi VJ,

Thank you for your help :) Where can I find the definitions of constants
like WM_SYSCOMMAND and SC_CLOSE, maybe there are other const that I
could use for future process, I find them useful.

Thanks,
Marty

VJ wrote:

Here is a sample to check if X button is clicked...

public const int SC_CLOSE = 61536;

public const int WM_SYSCOMMAND = 274;

public bool _closeClick = false;

protected override void WndProc(ref Message m)

{

if ( m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE )

{

this._closeClick = true;

}

base.WndProc (ref m);

}

private Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)

{

if ( this._closeClick )

{

// X button clicked..

}

}

Sorry for the bad format.. my notepad is screwed up... and I had to
paste it directly from VS.NET code....

VJ

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote in message
Marty,

When you make a call to Close, why not set a flag in your form which
indicates that it is you who made the call. Then, in the on closing
event, you can check the flag. If it is set, you closed the form,
otherwise, the user did it, and you can perform whatever action you
need.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi,

How can I detect that a VS.NET2003 C# form is closed with the upper
right "X" button ?

This is because I want to add code to the myForm.OnClosing() event to
do a different process if a form is closed by the user (with the "X"
button) or if the form is closed with myForm.close().

Thank you!
Marty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top