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

C# - FormClosing event handling

Hey there,

I'm having a problem with this code snippet: (C#)

Expand|Select|Wrap|Line Numbers
  1. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  2.         {
  3.             e.Cancel = true;
  4.             OtherForm dialog = new OtherForm();
  5.             DialogResult result = dialog.ShowDialog();
  6.             if (result == DialogResult.OK)
  7.             {
  8.                 Application.Exit();
  9.             }
  10.         }
MainForm throws the event, doesn't close, and shows the dialog.
If I add a watch on result, I can see it gets "OK" as its value, however, the application does not close. Any thoughts?
Mar 20 '08 #1
3 5720
misza
13
Hi,

The proper way to control whether to close a form in FormClosing event is to set the "e.Cancel" property:
- to false when you want to allow it to close
- to true to prevent closing

so as I understand, you want to do something like this:
Expand|Select|Wrap|Line Numbers
  1. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  2. {
  3. OtherForm other = new OtherForm();
  4. DialogResult resutl = other.ShowDialog();
  5. if(result != DialogResult.OK)
  6. {
  7.    e.Cancel = true;
  8. }
  9.  
  10. }
  11.  
In other words you don't have to allow the form to close in FormClosing, just prevent it if needed.

Hope this solves your problem,
Michal
Mar 21 '08 #2
Thanks a lot for the reply. I actually found this out right after I posted it.
Apr 9 '08 #3
Plater
7,872 Expert 4TB
All Application.Exit() does it send a windows message to close the form....which it already does to get to that event.
Apr 9 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
0
by: Frank | last post by:
Hello, in the formClosing event I put an endEdit for the datagridview. Works fine. Except when the cursor is on the line for a new record. That gives errors because some fields are not filled (and...
1
by: e_man_online | last post by:
Hi, I have a VStudio 2003 project that had code like: Private Sub frmAnyForm_Closing(<parameters list...>) Handles MyBase.Closing and this project was converted to .NET 2.0 Everything...
4
by: Academic | last post by:
I read the Help and some of the many Google hits I got but can't find out the difference between MyBase.Closing and MyBase.FormClosing Can anyone tell me? Thanks
13
by: Academic | last post by:
I have a MDI form, sometimes child forms and sometimes forms that are neither If I close the app the child forms closing and closed event happens followed by the Mdi form receiving the...
1
by: dani kotlar | last post by:
Is it possible to suspend the closing of a form from the FormClosing event handler, or any other way? Thanks
8
by: AAaron123 | last post by:
If I show a form with ShowDialog and Dispose it FormClosing does not appear to run. At least I think that is true. If I want FormClosing and FormClose to run am I suppose to call the forms...
2
by: Anthony P. | last post by:
Hello Everyone, I'm writing an application that will reside in the system tray once it is configured. The application also has an exit button called, creatively, ExitButton. I'm in a bit of a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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...

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.