简体   繁体   中英

Win Form in front of parent form

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

" 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

您需要将新表单显示为当前表单的子表单:

newForm.Show(this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM