Reputation: 715
I have two Forms. (Form1 and Form2) Form1_load method, I call to create Form2.
However, Form2 is still beneath Form1. How do I get Form2 on top of Form1? I do not want to set form2.TopMost to true as it is full screen form and will deny user's tabbing. I tried focus(), it just won't bring Form2 to the front.
I do not want to hide Form1 as user might need to tab back to it.
Upvotes: 2
Views: 131
Reputation: 7468
You are opening your second Form too early. The Load event for the Form is fired before the form is exposed, this means that Form 2 is shown before Form1, and hence it is covered by Form1 when this is shown.
You can obtain what you want by opening Form2 when the Shown event of Form1 is fired.
Upvotes: 1
Reputation: 310
You can use the Form2.ShowDialog() method. Bear in mind this will not allow you to tab back to form 1 until you close Form2. If Form2 Depends on data from Form1 I would rather use ShowDialog() otherwise you will have to use threading and events to update Form2 depending on the event in Form1.
Upvotes: 0
Reputation: 50672
You could use SetForegroundWindow(form2.Handle)
You'll need this declaration:
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetForegroundWindow(IntPtr hWnd);
Because you are calling this from the Load handler you might be in a race condition when after the Load of form1, form1 is brought to the top.
Upvotes: 0
Reputation: 10644
Did you try: form2.BringToFront()
?
Edit:
You can also use form2.ShowDialog()
, this should show form at front without possibility to focus form1
Upvotes: 4