473,714 Members | 2,470 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 6445
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
5292
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
5026
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
4022
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
2128
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
3296
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
2188
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
1645
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
8802
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
8711
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7953
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
6634
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
4464
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4726
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2522
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2111
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.