NateShoffner
NateShoffner

Reputation: 16839

C# - How to deal with 2 "TopMost" Forms?

I have a parent form that is set to be TopMost and then I have another form that opens when a button is clicked. This child form is also set to be TopMost. The first issue I had was that when I opened the child form, the application would basically freeze because you couldn't access anything. I decided that instead of using ShowDialog() to open the child form, I would use Show(this). This did fix the initial problem but now I have a new one. The childforms start postition is set to be CenterParent and when I use Show(this), it doesn't work. Is there any way I can make the childform open while both it and the parent form are set to topmost while having the childforms start position set to CenterParent? Thank you.

Upvotes: 5

Views: 6205

Answers (3)

Shahryar
Shahryar

Reputation: 235

I've find something useful to share with you, guys. Instead following code

form2.TopMost = true;

use this code in main form:

form2.Owner = this;

If you use Form.TopMost property, the form will overlap all other non-topmost forms, but also those from other applications. Instead of this, set the Form.Owner property to the parent form – the one which should be under the form (e.g. the main form). G00d luck :)

Upvotes: 5

Ilya Khaprov
Ilya Khaprov

Reputation: 2524

Hmm. I've created To forms. Then i set TopMost = true on both. The i add button to first and wrote new Form2().ShowDialog();

And all just fine. Form2 active and clickable. Form1 not since ShowDialog was called

And second variant works fine. Form2 opened in center of the screen.

May be i misunderstood something?

Upvotes: 0

ChrisF
ChrisF

Reputation: 137188

You could try clearing the TopMost property of the parent form for the duration that the child form is visible.

This would solve the problem of which form should be top most, as there will only ever be one.

Upvotes: 3

Related Questions