Show a form if already is running - C#

Hemanth B 886 Reputation points
2021-11-21T07:24:48.943+00:00

Hi, let's just say I have Form1 and Form2. Form1 has been minimized and I have a button in Form2. Now clicking the button, it should check if Form1 is already running and it should show up/ restore Form1 from it's minimized state. How do I do that?

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-11-21T07:55:10.997+00:00

    If you do not have more than one Form1, then try one of solutions:

    Form1 f1 = Application.OpenForms.OfType<Form1>( ).SingleOrDefault( );
    if( f1 == null )
    {
        f1 = new Form1( );
        f1.Show( );
    }
    else
    {
        if( f1.WindowState == FormWindowState.Minimized ) f1.WindowState = FormWindowState.Normal;
        f1.Activate( );
    }
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.