Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bring my application window to the front? [duplicate]

Tags:

c#

.net

winforms

How to bring my application window to front? For example whan my app needs attention.

This is for my personal program. I need that functionality.

This is what I got. But it's NOT working 100% times.

public void BringToFrontToEnterCaptha() {     if (InvokeRequired)     {         Invoke(new Action(BringToFrontToEnterCaptha));     }     else     {         this.TopMost = true;         this.Focus();         this.BringToFront();         this.textBox1.Focus();         this.textBox1.Text = string.Empty;         System.Media.SystemSounds.Beep.Play();     } }  public void BringToBackAfterEnterCaptha() {     if (InvokeRequired)     {         Invoke(new Action(BringToBackAfterEnterCaptha));     }     else     {         this.TopMost = false;     } } 

And I call them from background worker.

BringToFrontToEnterCaptha(); while (!ready) {     Thread.Sleep(100); } BringToBackAfterEnterCaptha(); Thread.Sleep(300); 

And after pressing "Accept" button bool ready is set to true.

I works great but not always.

like image 322
Hooch Avatar asked Mar 12 '11 12:03

Hooch


People also ask

How can I bring my application window to the front?

"Activate" does make Windows bringing current form to front of any other application currently open within the O/S. On the other hand, "bring to front" only make desired form showing in front of other forms within current application (not O/S). Form.

How do I bring an application to the foreground?

Clicking a button on window A will give that windows thread foreground activation. If it calls SetForegroundWindow (or equivalent) on the other window, that window WILL be given the foreground. If, on the other hand, it simply sends a message to the other app, which tries to SetForeground on itself, that will fail.

How do you bring to front in Visual Basic?

In Visual Studio, select a control that you want to layer. On the Format menu, select Order, and then select Bring To Front or Send To Back.

How do I open a second form in Windows?

Now go to Solution Explorer and select your project and right-click on it and select a new Windows Forms form and provide the name for it as form2. Now click the submit button and write the code. When you click on the submit button a new form will be opened named form2.


2 Answers

Here is a piece of code that worked for me

this.WindowState = FormWindowState.Minimized; this.Show(); this.WindowState = FormWindowState.Normal; 

It always brings the desired window to the front of all the others.

like image 96
Shabbir Hussain Avatar answered Sep 22 '22 20:09

Shabbir Hussain


Use Form.Activate() or Form.Focus() methods.

like image 26
as-cii Avatar answered Sep 23 '22 20:09

as-cii