Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
2.65/5 (3 votes)
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;
f.BringToFront();
F.Topmost=true;

How to force the new form to be brought to front and focused on?
Posted
Updated 25-May-21 0:01am
v2
Comments
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    
added

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!
 
Share this answer
 
v2
Comments
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.
Adding Focus() call should do the trick.

C#
(f = new FormDefineWord()).Show();
f.WindowState = FormWindowState.Normal;
f.BringToFront();
f.Topmost=true;
f.Focus();
 
Share this answer
 
Hello,
I know it is a old post but I all the solutions given here didn't worked for me.

However i found a trick to make this works :
// keep the size status of the form when variable is created.
FormWindowState keep_in_memory = this.WindowState;  



private void MainPage_Resize(object sender, EventArgs e)
{
    if (this.WindowState == FormWindowState.Minimized)
    {
        Hide();
        // apply the last size status of the form.
        this.WindowState = keep_in_memory;
    }
    else
    {
        // if size is changed but not minimized, so keep the windows status.
        keep_in_memory = this.WindowState;
    }
}


private void notifyIcon_MP_MouseDoubleClick(object sender, MouseEventArgs e)
{
    this.Show();
    this.Activate();
}

The trick is to keep in a variable the status of the windows before it minimize.
Then, after the windows is hiden, set the form to the memorized status.
This will made the form reopen as previously.


Hope this will help some of you :)
 
Share this answer
 
v2
this works for me
VS 2010
assuming this is current form for which focus is needed.

this.Show();
     this.WindowState = FormWindowState.Normal;
     this.BringToFront();
     this.TopLevel = true;
     this.Focus();
 
Share this answer
 
v2
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900