Andy
Andy

Reputation: 531

How to top one form on another?

How to top one form on another in C#?

Upvotes: 0

Views: 291

Answers (3)

Mursa Catalin
Mursa Catalin

Reputation: 1449

you put this in Form 2

 public delegate void CheckedEventHandler(bool val);
 public event CheckedEventHandler Checked;

and on event Click OK on FORM 2

if (Checked != null) 
    Checked(yourvalue); // bool value

and now in FORM 1

using (Form2 form = new Form2())
{
form.Checked += form2_Checked;
form.ShowDialog();
form.Checked -= form2_Checked;
}

void form2_Checked(bool val)
{
// do whatever you want with your value (form FORM2) set TopMost 
}

Upvotes: 1

levi
levi

Reputation: 3521

You can change the modifier(in properties) of chakbox of Form2 to public, so you'll be able to access to the check box.

if you are using form2.ShowDialog() than you can set dialog result by checkbox.Checked

Upvotes: 0

KeithS
KeithS

Reputation: 71591

If Form 1 is already "on top", then setting the "TopMost" property won't change anything.

Also, by default forms "own" other forms that are created and Show()n by them. So if Form 1 creates and Show()s Form2, and Form2 isn't closed when you click "OK", then even though Form1 gets set to TopMost, because it owns Form2 and, by definition, "owned" forms always appear on top of their owner, Form2 will still appear on top of Form1.

Generally, you should not use "TopMost" unless you are showing a window that the user MUST see regardless of whatever else is going on. There is usually some other way to accomplish what you want (such as the BringToFront() method).

Upvotes: 0

Related Questions