473,385 Members | 1,356 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,385 software developers and data experts.

how to trap X button in windows form (c#)

RAJ
hi

plz tell me how to know "how window is going to close"...
i have to right code for X button of forms...

plz telll me
thanks

bye
Nov 15 '05 #1
5 73160
Too lazy? Took me 2 seconds to find the events on MSDN.

ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemwindowsformsformclas
sclosingtopic.htm
and
ms-help://MS.MSDNQTR.2003APR.1033/cpref/html/frlrfsystemwindowsformsformclas
sclosedtopic.htm
Form.Closing Event
See Also
Form Class | Form Members | System.Windows.Forms Namespace | IsMdiContainer
| OnClosing | Form Members (Visual J# Syntax) | Managed Extensions for C++
Programming

Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003
family

Language
a.. C#

b.. C++

c.. JScript

d.. Visual Basic

e.. Show All
Occurs when the form is closing.

[Visual Basic]
Public Event Closing As CancelEventHandler
[C#]
public event CancelEventHandler Closing;
[C++]
public: __event CancelEventHandler* Closing;
[JScript] In JScript, you can handle the events defined by a class, but you
cannot define your own.

Event Data
The event handler receives an argument of type CancelEventArgs containing
data related to this event. The following CancelEventArgs property provides
information specific to this event.

Property Description
Cancel Gets or sets a value indicating whether the event should be
canceled.

Remarks
The Closing event occurs as the form is being closed. When a form is closed,
all resources created within the object are released and the form is
disposed. If you cancel this event, the form remains opened. To cancel the
closure of a form, set the Cancel property of the CancelEventArgs passed to
your event handler to true.

When a form is displayed as a modal dialog box, clicking the Close button
(the button with an X at the upper-right corner of the form) causes the form
to be hidden and the DialogResult property to be set to DialogResult.Cancel.
You can override the value assigned to the DialogResult property when the
user clicks the Close button by setting the DialogResult property in an
event handler for the Closing event of the form.

Note When the Close method is called on a Form displayed as a modeless
window, you cannot call the Show method to make the form visible, because
the form's resources have already been released. To hide a form and then
make it visible, use the Control.Hide method.
CAUTION The Form.Closed and Form.Closing events are not raised when the
Application.Exit method is called to exit your application. If you have
validation code in either of these events that must be executed, you should
call the Form.Close method for each open form individually before calling
the Exit method.
If the form is an MDI parent form, the Closing events of all MDI child forms
are raised before the MDI parent form's Closing event is raised. In
addition, the Closed events of all MDI child forms are raised before the
Closed event of the MDI parent form is raised. Canceling the Closing event
of an MDI child form does not prevent the Closing event of the MDI parent
form from being raised. However, cancelling the event will set to false the
System.Windows.Forms.ClosingEventArgs.Cancel property of the
System.Windows.Forms.ClosingEventArgs that is passed as a parameter to the
parent form. To force all MDI parent and child forms to close, set the
System.Windows.Forms.ClosingEventArgs.Cancel property to false in the MDI
parent form.

For more information about handling events, see Consuming Events.

Example
Form.Closed Event
See Also
Form Class | Form Members | System.Windows.Forms Namespace | IsMdiContainer
| OnClosed | Load | Form Members (Visual J# Syntax) | Managed Extensions for
C++ Programming

Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows
2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003
family

Language
a.. C#

b.. C++

c.. JScript

d.. Visual Basic

e.. Show All
Occurs when the form is closed.

[Visual Basic]
Public Event Closed As EventHandler
[C#]
public event EventHandler Closed;
[C++]
public: __event EventHandler* Closed;
[JScript] In JScript, you can handle the events defined by a class, but you
cannot define your own.

Event Data
The event handler receives an argument of type EventArgs.

Remarks
This event occurs after the form has been closed by the user or by the Close
method of the form. To prevent a form from closing, handle the Closing event
and set the Cancel property of the CancelEventArgs passed to your
event-handling method to true.

You can use this event to perform tasks such as freeing resources used by
the form and to save information entered in the form or to update its parent
form.

CAUTION The Form.Closed and Form.Closing events are not raised when the
Application.Exit method is called to exit your application. If you have
validation code in either of these events that must be executed, you should
call the Form.Close method for each open form individually before calling
the Exit method.
If the form is an MDI parent form, the Closing events of all MDI child forms
are raised before the MDI parent form's Closing event is raised. In
addition, the Closed events of all MDI child forms are raised before the
Closed event of the MDI parent form is raised.

For more information about handling events, see Consuming Events.

"RAJ" <tr*****@hotmail.com> wrote in message
news:e8**************@tk2msftngp13.phx.gbl...
hi

plz tell me how to know "how window is going to close"...
i have to right code for X button of forms...

plz telll me
thanks

bye

Nov 15 '05 #2


hi Jack Meyhoff

i had seen these MSDN pages but i m still confused
how i will know this is X button of form or something else...

bye

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
the [X] button is the same as WM_SYSTEM -> SC_CLOSE in win32 messaging.

"RAJ T" <tr*****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...


hi Jack Meyhoff

i had seen these MSDN pages but i m still confused
how i will know this is X button of form or something else...

bye

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4
I mean it is the same as WM_SYSCOMMAND -> SC_CLOSE win32 event message.
"RAJ T" <tr*****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...


hi Jack Meyhoff

i had seen these MSDN pages but i m still confused
how i will know this is X button of form or something else...

bye

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #5
http://beta.experts-exchange.com/Pro...ages/C_Sharp/Q
_20407745.html

To quote from that page.

--
To hide the window when the user presses the close button (X) subscribe to
the Form's Closing event...

(C#)
this.Closing += new
System.ComponentModel.CancelEventHandler(this.Form 1_Closing);

Then in the event handler hide the form, and cancel the close... Be careful
as this code alone will prevent you from ever closing the application,
somewhere you need to provide a way of closing the form, maybe use a flag
which can be tested in this event handler to determine whether or not the
event should be cancelled.

(C#)
private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// Hide the form...
this.Hide();

// Cancel the close...
e.Cancel = true;
}

You might also want to look at the following question/answer which describes
how to put an icon in the system tray
http://www.experts-exchange.com/Prog...ges/C_Sharp/Q_
20397858.html

As for the second part of your question...

The Form.Close() method will simply close that form and all resources
created within the object are closed and the form is disposed.

The Application.Exit() method informs all message pumps that they must
terminate, and then closes ALL application windows after the messages have
been processed. It is usually recommended that Form.Close() is called for
each form individually before calling Application.Exit().

If you need to get out of an application quick and don't care about cleaning
up (closing files, etc) then you can use Application.Exit(). The more usual
case would be to do a controlled Form.Close() of the main Form letting the
application come to a natural end.
--

"Jack Meyhoff" <po********@127.0.0.2> wrote in message
news:ur**************@TK2MSFTNGP09.phx.gbl...
the [X] button is the same as WM_SYSTEM -> SC_CLOSE in win32 messaging.

"RAJ T" <tr*****@hotmail.com> wrote in message
news:uq**************@TK2MSFTNGP12.phx.gbl...


hi Jack Meyhoff

i had seen these MSDN pages but i m still confused
how i will know this is X button of form or something else...

bye

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 15 '05 #6

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

Similar topics

1
by: gregory_may | last post by:
This code seems to "work" but I get the following errors: An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll then this one: An unhandled...
4
by: Arif Çimen | last post by:
Hi to everybody, I have chnged a button text in design mode. But After compiling and executing the program the text of the button do not change to new value. Any Ideas? Thaks for helps.
5
by: Wonder | last post by:
How can I create or use the msgobx to show a message without a default button. The user has explicity to click on the button, so the msgbox closes it. Thanks,
10
by: john | last post by:
In a form I'm trying to make a search field to be able to quickly find a record on ID-number. User enters a number and presses enter to move to that record. How do I trap for the enter-keystroke...
6
by: Robert Dufour | last post by:
On my form if the user clicks the upper right hand corner to close the form I want to trap that event and do a check to see if closing is allowed, if not, I want to stop the form closing action. ...
8
by: =?Utf-8?B?RyBIdXN0aXM=?= | last post by:
This is the 2nd time posting so sorry for duplications. I am using VB.NT 2005 & a standard Combobox. I've been wracking my brain over this problem for a over a month & cannot seem to find a way to...
6
by: John | last post by:
In a form how I can I trap for the action that a user opens the built in 'find dialog'? I would like to trap for it and before the dialog shows up, I would like to execute some code, and after that...
4
by: leeg | last post by:
Hi all, I am having trouble finding out how to stop a new record showing on my form when there are no records showing in my underlying query Query criteria is populated from a form text box and...
3
by: Z.K. | last post by:
I have a problem trapping the return key. I thought if I added the forms keydown event handler that this would do what I want. It did not even go into the keydown event handler when I pressed...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.