MDI Children Dialog Forms?

G

Gordan A Ziza

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.

BR,

GAZ
 
G

Gregory Gadow

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.
 
J

Jeffrey Tan[MSFT]

Hi Gordan,

Thanks for your post.

The solution provided by Gregory Gadow may meet your need, however, I have
not given it a try. Below is another sample implementing of mine:

ShowDialog() method is not allowed to be called on a mdi child form(Form1),
so we may implement the modal dialog logic ourselves. .Net ShowDialog()
actually just disable its owner form so that the owner form can not get any
mouse/keyboard input and become gray to achieve this effect.
So we can do the same thing: just disable Form1 when showing form2, then
enable the Form1 before closing, like this:
//MDI parent use a menuitem to show Form1 as MDI child
private void menuItem2_Click(object sender, System.EventArgs e)
{
Form1 f=new Form1();
f.MdiParent=this;
f.Show();
}

//Form1's button just shows the Form2
private void button1_Click(object sender, System.EventArgs e)
{
Form2 f=new Form2();
f.MdiParent=this.MdiParent;
f.modalparent=this; //use Form2's public field modalparent to store the
reference to form1
f.Show();
this.Enabled=false;
}

//Form2's code
public Form1 modalparent=null;
private void Form2_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
if(modalparent!=null)
{
this.modalparent.Enabled=true;
}
}

This works well on my side. I have created a sample for this, if you need
my little test sample, please feel free to tell me, I will attach it in a
further reply. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
G

Gordan A Ziza

Thanks a lot. Works like a charm. Quite simple when you look at it.

Thanks once again.

GAZ
 
G

Gordan A Ziza

Hello Jeffrey,

Thanks for the answer. Works as well as Gregory's.

My best regards,

GAZ
 
J

Jeffrey Tan[MSFT]

It's my pleasure to help you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top