In case you don't already know, a 'modeless' dialog is a window that is always on top of its parent window but activity can still take place within the parent window, kind of like a toolbar in photoshop (whereas a 'modal' dialog is a window that stops any activity in its parent window until the dialog is dismissed, like an open file dialog).
I've often wondered (though never pursued) how you create modeless dialogs in WinForms. Today I found out so I thought I'd share it with you. Which is nice. You're probably aware of the boolean
TopMost property of a form, but that's no good. Setting this to true simply puts your form
on top of everything, like Task Manager. If you have any manners, you won't use this willynilly.
It's simply a matter of setting the
Owner property of the form, to the form you want to always be on top of. If that makes sense.
Here's a sample app:

and some button click events...
// Launch Normal Window
private void _btnNormal_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Text = "Normal Window";
form.Show();
}
// Launch Modeless Window
private void _btnModeless_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Text = "Modeless Window";
form.Owner = this; // we want the new form to float on top of this one
form.Show();
}
// Launch Modal Window
private void _btnModal_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Text = "Modal Window";
form.ShowDialog();
}
And this is what we get if we launch a normal form and a modeless form

Note that the main form has focus and that the normal window has dropped to the back. However, the Modeless form is still on top
but we can still access the other forms. Let's try opening a Modal dialog.

Now the Modal dialog comes straight to the top and we can't access any of the other forms until this form is dismissed.
and?
So, why do I think this is even remotely interesting? Many LoB (Line of Business) applications have child forms that are driven from an original form that contains some kind of menu. If this is an approach you're considering, you should try running your child windows as modeless forms of the window containing the menu. Have only the menu window
ShowInTaskbar and you may be able to avoid that "attack of the windows" feeling. It's not
always the best choice but definitely one to bear in mind.

Post By
Josh Twist
11:17 AM
24 Jan 2007
» Next Post:
Easy Settings in WinForms
« Previous Post:
AJAX ASP.NET Extensions released
Comments are closed for this post.