Click here to Skip to main content
14,356,134 members
Rate this:
Please Sign up or sign in to vote.
See more:
When the user has the program minimized and presses F3 a global hook keys gets the press and translation window should be made and brought to front.

These don't always work:

(f = new FormDefineWord()).Show();
f.WindowState = FormWindowState.Normal;

How to force the new form to be brought to front and focused on?
Updated 23-May-18 6:33am
BillWoodruff 17-Feb-15 9:38am
"These don't always work:" what exactly do you observe that leads you to this conclusion. By "program minimized" do you mean the MainForm has been minimized and is visible as an Icon in the system-tray ? or ... ?
john1990_1 17-Feb-15 9:40am
the program is minimized to taskbar not system tray, the program detects F3 globally, and this code is run in order to show the user a form to enter a term to translate, the form opens but isn't brought to front nor focused on.

I added this.Topmost=true; and still not brought to front always.
Ramza360 17-Feb-15 10:44am
Did you try to specifically call Focus or Activate on the form, say after f.Topmost = true?
john1990_1 17-Feb-15 10:49am
Ramza you're right, submit answer to choose you best answer.
Ramza360 17-Feb-15 10:51am
Rate this:
Please Sign up or sign in to vote.

Solution 2

Try use a timer. First only show the window and activate a timer to run in about 50 or 100ms. Then in the timer event you call the BringToFront or set it to TopMost or whatever you like best.

The problem is that you create the window and try to set its properties in the same method. Calling BringToFront does not work because there is no form yet. This has to go through the same event looping to start up. After that you can change its properties.

Good luck!
john1990_1 17-Feb-15 9:56am
i tried using them is the Shwon method in the form and still doesn't work
E.F. Nijboer 17-Feb-15 9:57am
But that isn't a timer. The form needs to be completely ready. Other option is to create the form and hide it (instead of recreating it)
john1990_1 17-Feb-15 10:02am
didnt understand the other option
E.F. Nijboer 17-Feb-15 12:23pm
You now create a new FormDefineWord form each time the user presses F3. You could create this form once and hide it instead of recreating.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Adding Focus() call should do the trick.

(f = new FormDefineWord()).Show();
f.WindowState = FormWindowState.Normal;
Rate this:
Please Sign up or sign in to vote.

Solution 4

this works for me
VS 2010
assuming this is current form for which focus is needed.

     this.WindowState = FormWindowState.Normal;
     this.TopLevel = true;
CHill60 24-May-18 5:26am
The only thing you have changed from the Accepted Solution posted 3 years ago is to change Topmost to TopLevel which will unnecessarily change the behaviour of the OP's form

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100