Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Jun-16 3:51am    
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
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900