Win Form in front of parent form

2019-08-15 19:54发布

问题:

I have a form containing a button to create a new derived class form. In the callback for this button, I have the code

 AnotherFormClass newForm= new AnotherFormClass();
 newForm.Show();

When the button is clicked and this runs, the newForm momentarily flashes up in front of the original form on my screen, but then the original form comes back to the front. I don't want to use brute force TopMost() it to force newForm to always be in front. Plus, I'm not sure why I'm seeing this behaviour. Could anyone help please? I see there is an article mentioning a slightly similar problem here - Parent form is bringing to front when the menu strip of a child form is clicked but this is for .NET 4.5 and I've encountered this behaviour in 4.0 before too.

Thanks, Chris

回答1:

" the newForm momentarily flashes up in front of the original form on my screen, but then the original form comes back to the front."

If you mean that you always need the sub-form to be in front

use Form.ShowDialog() method, which will keep the sub-form in front until you close it.

In your case

 var newForm= new AnotherFormClass();
 newForm.ShowDialog(this);

Read more about Form.ShowDialog Method



回答2:

You need to show the new form as a child of the current form:

newForm.Show(this);


标签: c# forms