473,758 Members | 4,381 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trapping Closing event when a user clicks on the close (red "X" on frame)

RSH
I am trying to figure out how I can trap the Closing Event that occurs when
a user attempts to close the window by clicking on the red"X".

I tried this code which I found online but nothing happens.

What do I need to do to trap and give the ability to save changes before the
window closes?

Thanks,
Ron

private void frmDataEditGrid _Closing(object sender, CancelEventArgs e)

{

e.Cancel = true;

if (MessageBox.Sho w("Do you want to save changes to your text?", "My
Application",

MessageBoxButto ns.YesNo) == DialogResult.Ye s)

{
e.Cancel = true;
}

}
Jan 3 '06 #1
4 6448
The code as posted won't work, no (it always stops the close, as e.Cancel is
always set to true), but the Closing event is fine;

The following, added to a form, works for me:

public Form1() { // ctor
// initialise and hook up the closing event (event could also be
attached in the IDE / InitializeCompo nent, but I like it here...)
InitializeCompo nent();
FormClosing += new FormClosingEven tHandler(Form1_ FormClosing);
}

void Form1_FormClosi ng(object sender, FormClosingEven tArgs e) { //
event handler
bool cancel = false; // let the form close by default
if(MessageBox.S how("Cancel?"," Test",MessageBo xButtons.YesNo) ==DialogResult. Yes)
{
cancel = true; // don't close...
}
e.Cancel = cancel;
}

Marc

"RSH" <wa************ *@yahoo.com> wrote in message
news:u4******** ******@TK2MSFTN GP10.phx.gbl...
I am trying to figure out how I can trap the Closing Event that occurs when
a user attempts to close the window by clicking on the red"X".

I tried this code which I found online but nothing happens.

What do I need to do to trap and give the ability to save changes before
the window closes?

Thanks,
Ron

private void frmDataEditGrid _Closing(object sender, CancelEventArgs e)

{

e.Cancel = true;

if (MessageBox.Sho w("Do you want to save changes to your text?", "My
Application",

MessageBoxButto ns.YesNo) == DialogResult.Ye s)

{
e.Cancel = true;
}

}

Jan 3 '06 #2
"Marc Gravell" <mg******@rm.co m> a écrit dans le message de news:
Oz************* *@TK2MSFTNGP14. phx.gbl...

Simplified to :

void Form1_FormClosi ng(object sender, FormClosingEven tArgs e)
{
e.Cancel = MessageBox.Show ("Cancel?","Tes t",MessageBoxBu ttons.YesNo) ==
DialogResult.Ye s)
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 3 '06 #3
Two quick things... when you say that nothing is happening... I am guessing
you are not even seeing the message box. If that is the case, you need to add
a bit of code to have your function be called when the event is raised.

The easiest way to do that would be to view your form in the form designer,
and in the Properties window click the Events button (has the lightning bolt
icon, and locate the Closing event. Finally, use the drop down to select your
handler, frmDataEditGrid _Closing.

Second, remove the first instance of ‘e.Cancel = true’ which is being called
no matter what. Without it, the option you choose from the prompt will cause
the form to close or not.

Brendan
"RSH" wrote:
I am trying to figure out how I can trap the Closing Event that occurs when
a user attempts to close the window by clicking on the red"X".

I tried this code which I found online but nothing happens.

What do I need to do to trap and give the ability to save changes before the
window closes?

Thanks,
Ron

private void frmDataEditGrid _Closing(object sender, CancelEventArgs e)

{

e.Cancel = true;

if (MessageBox.Sho w("Do you want to save changes to your text?", "My
Application",

MessageBoxButto ns.YesNo) == DialogResult.Ye s)

{
e.Cancel = true;
}

}

Jan 3 '06 #4
RSH
Thanks alot. I am just starting to understanding wiring events...this was
very helpful. THANKS!
"Marc Gravell" <mg******@rm.co m> wrote in message
news:Oz******** ******@TK2MSFTN GP14.phx.gbl...
The code as posted won't work, no (it always stops the close, as e.Cancel
is always set to true), but the Closing event is fine;

The following, added to a form, works for me:

public Form1() { // ctor
// initialise and hook up the closing event (event could also
be attached in the IDE / InitializeCompo nent, but I like it here...)
InitializeCompo nent();
FormClosing += new FormClosingEven tHandler(Form1_ FormClosing);
}

void Form1_FormClosi ng(object sender, FormClosingEven tArgs e) { //
event handler
bool cancel = false; // let the form close by default

if(MessageBox.S how("Cancel?"," Test",MessageBo xButtons.YesNo) ==DialogResult. Yes)
{
cancel = true; // don't close...
}
e.Cancel = cancel;
}

Marc

"RSH" <wa************ *@yahoo.com> wrote in message
news:u4******** ******@TK2MSFTN GP10.phx.gbl...
I am trying to figure out how I can trap the Closing Event that occurs
when a user attempts to close the window by clicking on the red"X".

I tried this code which I found online but nothing happens.

What do I need to do to trap and give the ability to save changes before
the window closes?

Thanks,
Ron

private void frmDataEditGrid _Closing(object sender, CancelEventArgs e)

{

e.Cancel = true;

if (MessageBox.Sho w("Do you want to save changes to your text?", "My
Application",

MessageBoxButto ns.YesNo) == DialogResult.Ye s)

{
e.Cancel = true;
}

}


Jan 3 '06 #5

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

Similar topics

4
5295
by: John Benson | last post by:
Hi, I'm using Tkinter and would like to know how to replace the red "Tk" logo in the left corner of the title bar of the root window with some other icon. Another question for curiosity's sake: I've often seen Tkinter referred to as the "standard" Python GUI. I assume that this is because it may be the oldest and perhaps the most cross-platform. Is that all that can be said for it? I've already heard about the Tkinter interpretive...
13
5027
by: elad | last post by:
Hi The Menu doesn't work properly when I have 2 frame and the Menu popup frame=document target frame, when I choose item in the menu the doc opened and the menu get stuck. Here is the code I'm using: FrameSet ------------------------------------------------------- <html>
10
4029
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms, remove the controls on each of them, and then close the form, but this code should execute only...
5
2131
by: gsb58 | last post by:
Hi! In my application I have code that will give the user a choice when he click's the close button on the ToolBar1. However, when he clicks the close button of the form, the form will shut down even if he chooses the cancel button of the messagebox that pop's up. Now...if he, on the other hand, chooses ALT+F4, then I catch this in
2
3298
by: polocar | last post by:
Hi, suppose that you have a C# form with two buttons, that are the classical "btnOk" and "btnCancel" (besides them, of course in the form there can be many other controls). When the user clicks on btnOk, the program makes some confirm operations and closes the form. When the user clicks on btnCancel, the program makes some cancel operations and closes the form. My problem is that, when the user clicks on the "X" button of the form...
8
2194
by: Mel | last post by:
I need to draw a red frame around the element under current pointer. I have seen it done in debuggers, but need to have it in my application, without the need to download and install applications. Any help is appreciated
1
1647
by: Brian Kendig | last post by:
How do I take an index of a literal array, instead of a variable? That is, say I want to get the short name of a month. Can I do something like: $monthname = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"); or do I always have to do:
3
1475
by: Gestorm | last post by:
It seems that the standard c library doesn't give any support on this. I googled this question and found that the code would not be portable. Then how can I do this under linux ? Thanks! ^_^
0
9489
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9906
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3399
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.