An example of MDI form closure

Source: Internet
Author: User

Due to busy work, I haven't been here for a long time.

Background:

One of the MDI child forms is a navigation form, which defines CLOSING event processing: checks whether the close mark of the form is allowed. If not, cancels the close operation, this ensures that the navigation form is permanently present without being closed.


Problem:

When the MDI container form is closed, the close event of one of the Child forms is canceled, so the close operation fails, as a result, you can only call the Exit method of Application to close the MDI main form.

Solution:

When the MDI subform is closed, the close operation of the form may be canceled due to the closed control of the navigation form. Therefore, when the main form is closed, set the close flag of the navigation form to allow closing, so that the main form will not be canceled because the subform is disabled. However, the experiment results are not feasible: The CLOSING events of all sub-forms are triggered before the CLOSING events of the main form. When the flag can be disabled in the main form, the main form is invalid. I feel very frustrated. I cannot ask the user to close the operation twice, right? It is not ideal to use Application. Exit for processing. Therefore, you can set the flag of the navigation form and call the close function again.

After tracking and processing, it is found that the main form and all sub-forms share the same CLOSING event parameter (System. componentModel. cancelEventArgs), so as long as any form (including the main form and sub-form) cancels the Close event, the entire close operation fails. For this reason, set CANCEL = false in the CLOSING event of the main form to easily solve the problem (because the CLOSING event of the main form is always the last triggered ).

The preceding solution has another vulnerability: in addition to the navigation window, if other forms do need to be canceled, the Operation will fail. You can use the following two solutions to solve this problem:

1 --

In the CLOSING process of the main form, force the CANCEL flag to reset, and then execute the check that each subform does allow CLOSING, and then set the CANCEL flag.

This solution is not good because the main form must know whether the Sub-form defines the specified function, resulting in dependency on the Implementation type of the sub-form.

2 --

Based on the problems in solution 1, consider whether the flag of each subform can be closed and placed in the Tag attribute, this problem can be solved by reaching this agreement between the subform and the main form.

Related code:

Subform Closing event code:

If (CanClose)

{

If (Tag = null)

{

Tag = true;

}

Else

{

ArrayList arrTmp = new ArrayList ();

ArrTmp. Add (true );

ArrTmp. Add (Tag );

Tag = arrTmp;

}

}


Main form Closing event processing code:

E. Cancel = false;

Foreach (Form frmTmp in this. mdichil.pdf)

{

If (frmTmp. Tag = null)

Continue;

If (frmTmp. Tag is bool)

{

If (bool) frmTmp. Tag ))

{

E. Cancel = true;

Break;

}

FrmTmp. Tag = null;

}

If (frmTmp. Tag is ArrayList)

{

ArrayList arrTmp = frmTmp. Tag as ArrayList;

If (arrTmp. Count! = 2 |! (ArrTmp [0] is bool ))

Continue;

FrmTmp. Tag = arrTmp [1];

If (bool) arrTmp (0 ))

{

E. Cancel = true;

Break;

}

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.