5

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.

Ahmad Sadigh
  • 63
  • 1
  • 7
  • I've edited your question to try to make it more clearer but it still is unclear, what do you mean "mouse's position y=0" – Sayse Aug 13 '15 at 06:45
  • you have to use mdiparent and mdichild to different classes and show them in front , activate or whatever. – Sakthivel Aug 13 '15 at 06:49
  • @Sayse: It is not important at all. I just wanted to bring an example of an event. – Ahmad Sadigh Aug 13 '15 at 06:55

3 Answers3

5

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;
    }
}
Ahmad Sadigh
  • 63
  • 1
  • 7
2

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

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

Fabjan
  • 13,506
  • 4
  • 25
  • 52
0

Try to use this

yourForm.TopMost = true;
Smudoo
  • 562
  • 3
  • 11