Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring a Windows Form on top? [duplicate]

Tags:

c#

winforms

I am developing a windows form application in C#. This application shows some text on the Form when a special kind of external event occurs (For example suppose that I want to write "Mouse is on upper line" on form when in the mouse's position y=0). I need to bring the Form to top of every other window when the event occurs.

like image 240
Ahmad Sadigh Avatar asked Aug 13 '15 06:08

Ahmad Sadigh


People also ask

How do you make a form always on top?

You can bring a Form on top of application by simply setting the Form. topmost form property to true will force the form to the top layer of the screen, while leaving the user able to enter data in the forms below. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.

How do I duplicate a Windows Form?

Press "Ctrl" and drag your mouse to duplicate a existing form class file. Then exlude the existing form class file from the project.

How do I create a ProgressBar in Windows Forms?

To create a ProgressBar control at design-time, you simply drag a ProgressBar control from the Toolbox and drop onto a Form in Visual Studio. After you the drag and drop, a ProgressBar is created on the Form; for example the ProgressBar1 is added to the form and looks as in Figure 1.


2 Answers

Use this in you form class:

public void BringToTop()
{
    //Checks if the method is called from UI thread or not
    if (this.InvokeRequired)
    {
        this.Invoke(new Action(BringToTop));
    }
    else
    {
        if (this.WindowState == FormWindowState.Minimized)
        {
            this.WindowState = FormWindowState.Normal;
        }
        //Keeps the current topmost status of form
        bool top = TopMost;
        //Brings the form to top
        TopMost = true;
        //Set form's topmost status back to whatever it was
        TopMost = top;
    }
}
like image 56
John Steven Avatar answered Sep 28 '22 15:09

John Steven


According to this source you can simply do form.Activate();

P.S. You may find this information useful too.

like image 35
Fabjan Avatar answered Sep 28 '22 15:09

Fabjan