Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form
Cancel close on an MDI child form Cancel close on an MDI child form
Cancel close on an MDI child form
Go Back  Xtreme Visual Basic Talk > > > Cancel close on an MDI child form


Reply
 
Thread Tools Display Modes
  #1  
Old 05-19-2012, 10:05 PM
celtic celtic is offline
Centurion
 
Join Date: Mar 2009
Posts: 132
Default Cancel close on an MDI child form


Hi all.

OK this seems a little different and I'm not sure how to procede.

what I want to do is cancel a MDI child form closing when the 'X' at the top right hand side of the form is clicked.

So, I use e.Cancel = True in the MDIchild_FormClosing event.

However, the parent will not close if the 'X' button is clicked, and I noticed that the FormClosing event of the child handles 'Me', which I assume is the parent.

Changing the handles 'Me' to handles 'nameOfChildForm' only generates an error Error: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

Even newer to MDI than VB, so now I'm a bit lost.

Some help with explanation would be appreciated muchly :-)
Reply With Quote
  #2  
Old 05-20-2012, 12:29 PM
passel's Avatar
passelCancel close on an MDI child form passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

Me would refer to the current object that the code is in, which would be your MDIchild form in the MDIchild form, or your main form, in the main form code.
In order for your main form to close, all the child forms have to close.
Since you have code in the MDIchild form to stop if from closing, then your main form will not close.

Perhaps you want to check the reason for closing, and only not close the child window when the user tries to close it, but allow it to close when the main form is closing, or the application is exiting, etc...
Code:
  Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    If e.CloseReason = CloseReason.UserClosing Then  'If user closing this window
      e.Cancel = True  'cancel, otherwise allow the window to close for any other reason
    End If
  End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 05-20-2012, 03:18 PM
hDC_0Cancel close on an MDI child form hDC_0 is offline
Contributor

* Expert *
 
Join Date: Feb 2004
Posts: 560
Default closing MDI and child windows, parental closing awareness, saving data and/or state.

passel is right that in your form closing you have to be able to handle
any condition where the child window (and or the "document" its handling)
are not in a condition to close (where closing is advisable without checking with
the user --for instance having a dialog box asking
"Do you want to save any changed data values?")

However some MDI newbies include code that tries to auto-close all child
windows when the user tries to close the whole MDI application down.

The child window should be "aware" that one of the reasons it may be
requested to try and close is because the parent may be closing.

I usually handle this situation with a m_blnMDIrunning boolean.

This is usually tied in with a user option/preference to
"auto-save all changed data upon closing", where the user doesn't
have to go through a whole cascade to "Do you want to save data.."
dialogs for each of the child forms associated with open documents (which
could number in the hundreds if the user is allowed to generate child forms
by opening documents dynamically at runtime).

In such a case, with the auto-save option enabled, all child form closing
dialogs are suppressed and a background file update of all data changed in
any of the child forms is automatically handled in each child forms closing event.

Even if there is no data to be saved, you may want to update the MRU
(most recent used) document list (if you haven't already as the documents where being opened),
and save the "state" (size and positioning) of the child form windows,
as well as any "skinning" changes that were made by the user
during that runtime instance.

Last edited by hDC_0; 05-20-2012 at 03:24 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form Cancel close on an MDI child form
Cancel close on an MDI child form
Cancel close on an MDI child form
 
Cancel close on an MDI child form
Cancel close on an MDI child form
 
-->