Gordan said:
Hello everyone,
We are developing a MDI application and what we would require is the
following:
1. To open a MDI child form as Form1
2. To open another MDI child form as Form2 that would act as a dialog
form for the Form2
3. Form2 must not prevent any other MDI forms from opening or otherwise
prevent application from working properly
4. Form2 must only prevent Form1 (or any of the controls on it) from
getting focus (as a normal Dialog would for an application)
Nifty trick. We've seen it in the Microsoft Navision but haven't got a clue
how to actually do it. Any help appreciated.
I've implemented similar forms. I don't have VS in front of me, but my approach
to your problem would be along the lines of....
* Put a private Form2 declaration in the Form1 class, ie give each instance of
Form1 its own Form2.
* Add two public methods to Form2, one to display the form (let's call it
Display) and one to hide it (call it PutAway), and expose a public boolean
property, IsActivated. Along with any other setup code, Display should set
IsActivated to True and call Me.Show (not Me.ShowDialog.) PutAway should set
IsActivated to False and call Me.Hide. In either method, you might want to
reset the controls and variables to a default state. IsActivated should default
to False, either implicitly in declaration or manually in Form2's constructor.
* Add an event handler to Form2 that catches the Closing event (in .NET 2.0,
the FormClosing event.) Have the handler cancel the close (which would dispose
of the instance Form1 is holding) and instead call Me.Hide.
* Declare a private Control variable in the Form2 class.
* Add an event handler to Form2 that stores the control on Form2 that is losing
the focus in the private Control variable. Use AddHandler to point the
LostFocus event of every control on Form2 that can receive focus to this
handler. The end result is that when any focusable control on Form2 loses
focus, the form makes a note of it.
* Add a public function to Form2, ReEnter, that will reenter the form and
return focus to the private Control variable. If you are the paranoid type, you
can have ReEnter default to Display if the form is not active, or do nothing
and return.
* Add an event handler to Form1 that catches the form's GetFocus event. If it's
associated Form2.IsActivated is True, then call Form2's ReEnter method.
That should do it. I suggest the Display, PutAway and IsActivated stuff because
..NET garbage collection is pretty flakey; testing the variable to see if it is
Nothing or something is unreliable. Taking manual control is kludgy but it does
give predictable results.
The end result: You can have multiple Form1s, each with its own associated
Form2. If a Form1 has a Form2 active, any effort to set the focus to the Form1
will instead be set to the correct Form2.