Home > Ask the Microsoft .Net Development Experts > C# Development Questions & Answers > Windows Forms: Overriding the OnClosing method
Ask The Win Development Expert: Questions & Answers
EMAIL THIS

Windows Forms: Overriding the OnClosing method

Daniel Cazzulino EXPERT RESPONSE FROM: Daniel Cazzulino

Pose a Question
Other Win Development Categories
Meet all Win Development Experts
Become an Expert for this site


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


>
QUESTION POSED ON: 23 March 2005
Although I spent many years as a UNIX developer using C++ and X-Windows, I am new to MS Windows. I am working on a C# Window Forms app and I want to know how to trap the Window close event (when the user presses the 'X' button in the upper right corner of the form). I use validation methods on my text boxes and have not been able to figure out how to stop the validation event when the user presses the 'X?' Thanks R.B.

>

The most straightforward way to do it is to override the behavior of the OnClosing method on the Form. Basically, this method passes an argument to the active control to see if it validates, and the Cancel flag on the argument is set accordingly. By overriding the value set on that flag, you can always allow the form to close:

protected override void OnClosing(CancelEventArgs e)
{
    // Let the default behavior to happen.
    base.OnClosing(e);
    // Do not allow cancellation of the close operation.
    e.Cancel = false;
}


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
C# Development
Top C# expert advice
Using LoadRunner for testing in .NET
Take care with InitializeComponent
GroupBox inheritance
Escape from mailto:
Passing data between forms
Can I run a .NET app without installing .NET runtime?
Opening a link with parameters to a new window
Is it possible to open a child form in modal type?
Painting in the non-client area

WinForms development
Windows 7 goes RTM
VB code: Forms downloads
Design tool targets WinForms controls
Farpoint updates spreadsheet tools for Web, client apps
Windows Developments: Product news, November 2007
Infragistics updates NetAdvantage with PDF export engine, Vista UI elements
NetAdvantage for .NET adds dashboard, SEO tools
Perpetuum updates data visualization, BI reporting tools
Perpetuum updates dashboard building suite
VB code: New additions, June 2007

C# programming language
Mono 2.0 moves .NET apps to Linux - includes migration analyzer
LINQ beyond queries: Strong-typed refection
Assembly versioning in the .NET Framework 2.0
Book excerpt: Creating graphical output using the .NET Compact Framework
Visual Studio 2008 Learning Guide: C# 3.0
Simonyi firm to address divide between domain experts and developers
On Extension Methods in C# and .NET Framework 3.5
Generate RSA public and private keys, export to XML
Book excerpt: Upgrading to Visual Studio 2005
Learning .NET: Tips for getting started with .NET development

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
C#  (SearchWinDevelopment.com)
GLib  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice



Windows Development - White Papers, News and Expert Advice
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2000 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts