Hooch Hooch - 16 days ago 5
C# Question

How can I bring my application window to the front?

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.

Jon Jon
Answer

Use Control.BringToFront:

myForm.BringToFront();