Form.TopMost behaviour

G

Guest

I'm a bit confused regarding the behaviour when setting the Form.TopMost
property. If setting the property to 'true', does it make the form top-most
of all windows in all applications, or only the form's application ?
Searching google, I've found people claiming it's enough to set TopMost in
order for the form to be top-most of all windows in the system, and other say
that a call to Win API's SetWindowPos (with HWND_TOPMOST as a parameter) is
needed.

So, which is correct?
 
M

mabster

Yonatan said:
I'm a bit confused regarding the behaviour when setting the Form.TopMost
property. If setting the property to 'true', does it make the form top-most
of all windows in all applications, or only the form's application ?
Searching google, I've found people claiming it's enough to set TopMost in
order for the form to be top-most of all windows in the system, and other say
that a call to Win API's SetWindowPos (with HWND_TOPMOST as a parameter) is
needed.

So, which is correct?

TopMost sets the form to be the topmost form across the entire system.

There's another property, less well-documented, called TopLevel which
sets the form as topmost only within the current application.

(IIRC)
 
M

Michael C

Yonatan said:
I'm a bit confused regarding the behaviour when setting the Form.TopMost
property. If setting the property to 'true', does it make the form
top-most
of all windows in all applications, or only the form's application ?
Searching google, I've found people claiming it's enough to set TopMost in
order for the form to be top-most of all windows in the system, and other
say
that a call to Win API's SetWindowPos (with HWND_TOPMOST as a parameter)
is
needed.

So, which is correct?

Dunno, you could have tried it quicker than writing this message.
 
C

Chris Dunaway

mabster said:
There's another property, less well-documented, called TopLevel which
sets the form as topmost only within the current application.

Umm.... No.
From the docs:

A top-level form is a window that has no parent form, or whose parent
form is the desktop window. Top-level windows are typically used as the
main form in an application.
 
M

mabster

Chris said:
Umm.... No.


A top-level form is a window that has no parent form, or whose parent
form is the desktop window. Top-level windows are typically used as the
main form in an application.

I don't know how to respond to that. Did you test it? Make an
application with a Form1 and Form2, and drop two Buttons on Form1:

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TopMost = true;
f2.Show();
}

private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TopLevel = true;
f2.Show();
}

TopMost is topmost across the whole system. TopLevel is only "stay on
top" within the application.

The behaviour is just as I described.
 
C

Chris Dunaway

mabster said:
I don't know how to respond to that. Did you test it? Make an
application with a Form1 and Form2, and drop two Buttons on Form1:

private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TopMost = true;
f2.Show();
}

private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TopLevel = true;
f2.Show();
}

TopMost is topmost across the whole system. TopLevel is only "stay on
top" within the application.

The behaviour is just as I described.

When I clicked button1, Form2 appeared and was above all other forms as
expected. When I clicked button2, Form2 appeared again and seemed to
be above form1, but when I clicked form1, form2 was in the background
and not on top, I saw no special behavior. I added a third button with
this code:

private void button3_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.TopLevel = false;
f2.Parent = this;
f2.Show();
}

And the new form appeared *inside* form1 but was not necessarily on top
of other forms in the app.

I can only go by what the docs say and they do not mention anything
about z order in conjunction with the TopLevel property. TopLevel
refers to parentage and not z-order. In fact the default for all forms
is TopLevel = true.

If I am wrong about this, I hope someone with more knowledge will
elaborate.

Cheers,

Chris
 
M

mabster

Chris said:
When I clicked button1, Form2 appeared and was above all other forms as
expected. When I clicked button2, Form2 appeared again and seemed to
be above form1, but when I clicked form1, form2 was in the background
and not on top, I saw no special behavior.

Run it again but switch away from the application when it's running.

After clicking button1, Form2 will stay on top even when another
application has focus.

After clicking button2, Form2 will always be on top of Form1 but will
not stay on top of other applications.

As I said - TopMost is system-wide stay-on-top, but TopLevel is only
application-wide.
 
C

Chris Dunaway

mabster said:
Run it again but switch away from the application when it's running.

After clicking button1, Form2 will stay on top even when another
application has focus.

Yes.

After clicking button2, Form2 will always be on top of Form1 but will
not stay on top of other applications.

Not if I click Form1 first before switching away. In that case, the
first Form2 stays on top because it is TopMost but the second Form2
does *not* stay on top of Form1. Setting TopLevel=true when creating
the second form2 is redundant as the form is already TopLevel.
TopLevel has nothing to do with z-order.
 
M

mabster

Chris said:
Not if I click Form1 first before switching away. In that case, the
first Form2 stays on top because it is TopMost but the second Form2
does *not* stay on top of Form1. Setting TopLevel=true when creating
the second form2 is redundant as the form is already TopLevel.
TopLevel has nothing to do with z-order.

Oh, you made Form1 TopMost as well? I was only talking about a single
"stay-on-top" form in an app. Any more than one is just asking for
trouble, UI-wise, I think.
 
M

Michael C

mabster said:
Oh, you made Form1 TopMost as well? I was only talking about a single
"stay-on-top" form in an app. Any more than one is just asking for
trouble, UI-wise, I think.

I have to agree with Chris, setting TopLevel to true on my machine doesn't
make any difference. If I set Form2 to toplevel I can still click Form1 and
it will come to the front.
 
M

mabster

Michael said:
I have to agree with Chris, setting TopLevel to true on my machine doesn't
make any difference. If I set Form2 to toplevel I can still click Form1 and
it will come to the front.

Weird. I'm sure it didn't behave like that in my test. I might have to
re-create it since I didn't bother saving it yesterday. :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top