Click here to Skip to main content
13,903,133 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi, all

Currently, I study vb.net, and I am learning the code about closing the form but I get some issue when form processing and closing, I mean I need to show the message( "please wait , fetching data will complete soon"), If the process not yet to complete such as fetching data

Note: my English not strongest

thank you
kevin

What I have tried:

I need learn more , I hope this will be good information for new guy
Posted
Updated 28-Jun-16 21:50pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Subscribe the Closing event (WindowClosing or FormClosing) of the form. It comes with CancelEventArgs which have a Cancel property. Set that property to True when you cannot close the form.
   
Comments
   
I voted 4 in this case. The answer is not quite complete.

First, the option of using OnFormClosing is not mentioned, but it has certain benefits. More importantly, CloseReason property is important but is often forgotten, so it's important to point it out. Please see Solution 2.

—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

In addition to Solution 1:

An alternative to handling FormClosing, you can override the virtual method System.Windows.Forms.Form.FormClosing:
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.onformclosing%28v=vs.110%29.aspx.

The event arguments type is the same, FormClosingEventArgs, so you control the cancellation of processing the same way, via FormClosingEventArgs.Cancel:
FormClosingEventArgs Class (System.Windows.Forms)[^].

Note the you should better check up FormClosingEventArgs.CloseReason; usually, you need to prevent closing only when it's closing by the user via the form's control, so you have to check is the reason is CloseReason.UserClosing:
FormClosingEventArgs.CloseReason Property (System.Windows.Forms),
CloseReason Enumeration (System.Windows.Forms).

Overriding FormClosing has certain benefits over the event handling. Here is why: overridden code is simpler and more maintainable, and the need to create a derived type is not a hassle, because Form types are usually derived in the application anyway.

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190306.1 | Last Updated 29 Jun 2016
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100