Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello All,

I was wondering if you can help me with the following scenario:
- I have a MDI application, with the child forms opening / staying within the main parent form
- Open 2 child forms by opening a first child for (say ChildForm-A), then open another child form (say ChildForm-B)
- Now I'm filling data and processing stuff on ChildForm-B.
- Assume that some of the Data are invalid (they fail validation some how)
- Now try to switch forms leaving ChildForm-B and going to ChildForm-A
- The event "MdiChildActivate" is fired and I get to the event handler on the parent control.
- Once this event is fired, the "Leave" event is fired on ChildForm-B, and the validation kicks in.
- Only then I know that the Form is invalid, and I want to stay on it (ChildForm-B), however by now I'm already on the other form, since "MdiChildActivate" has been fired.
- Now, the only way I can stay on the invalid form, is to set it back as the active child

- Is there a way to prevent leaving the invalid form if I'm trying to switch between 2 MDI child forms without going to form A then manually set back the active child to form B

- Thank you in advance for all your help.
Posted
Updated 10-Aug-11 9:23am
v2

From MSDN:

When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl..::.ActiveControl property to the current form, focus events occur in the following order:

1.Enter
2.GotFocus
3.Leave
4.Validating
5.Validated
6.LostFocus

When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

1.Enter
2.GotFocus
3.LostFocus
4.Leave
5.Validating
6.Validated

If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

If the Cancel property of the CancelEventArgs is set to true in the Validating event delegate, all events that would usually occur after the Validating event are suppressed.


I believe that all ContainerControls have a method method for validating their own child controls.
 
Share this answer
 
Ask yourself the question why your application need to be an MDI application.

Then ask yourself if the way your want to use your MDI child correspond to the way a standard MDI application works.

MDI child in an application are intended to show multiple documents or multiple views of the same document.

Each MDI child should be independant of any other. If your application cannot works as an MDI application, then don't make a MDI application. Uses standard forms instead (and specify the owner so that they display above the main form).

Also display only one form at a time. If the first form is hidden (or disabled by having an owned child form displayed), then the user won't be able to go back to the first form without closing the second one.

If you try to works around intended way, then you ask for trouble and lot of work. If it is an MDI application, you will have to ensure that you properly handle all case including opening a new MDI child (from the menu), closing all child and such.
 
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