Modal Forms - Beginner Question

webbiz

Member
Joined
Oct 15, 2011
Messages
21
Programming Experience
10+
I'm just learning to program in C# via VS2010.

I've added a menu strip and added a menu item (Load Data).

For Load Data, I want to open another form (DataLoad) that holds the focus till I'm done using it.

My understanding is that it needs to be MODAL so that if I click on the form that has the menu that called it that it will not switch focus over to it.

Here is my code:

private void loadDataToolStripMenuItem_Click(object sender, EventArgs e)
{
DataLoad frmLoadData = new DataLoad();
frmLoadData.Show(this);


}

When I run the program and then click on the menu Load Data, it brings up my DataLoad form as expected.
However, if I click on my main form (that contains the calling menu item) the focus switches back to it.

Not what I want.

Apparently adding "this" was not enough. Just can't find an answer to this.

Sorry if this is the most basic question ever.

TIA
 
By passing 'this' to Show you have actually made it a modeless dialogue rather than a modal dialogue. An example of a modeless dialogue is the VS Find & Replace window. A modeless dialogue will remain on top of its owner without preventing access to it. It will also be minimised, restored and closed when its owner is.

To create a modal dialogue, you need to call ShowDialog rather than Show. You can still pass the current form as the owner but there's not much point. It's important to realise that a form displayed with ShowDialog will not be disposed when closed while one displayed with Show will be. That means that you have to dispose a modal dialogue yourself, which you should usually do with a 'using' statement:
using (var dialogue = new DataLoad())
{
    dialogue.ShowDialog();
}
It's also worth noting that ShowDialog returns a DialogResult value, which is generally used to indicate what button the user clicked on the dialogue.
 
Thank you jmcilhinney!

The .ShowDialog() did as prescribed.

Coming from the VB6 world, that particular method confused me.

:)
 
Back
Top Bottom