1

I have a button on Form1 that opens Form2(Leaderboard) if Form2 is not open. What I am trying to do is: On Button click, if Form2 is closed, open Form2. Else Form2 is open, bring Form2 to front.
With the below code, clicking the button when leaderboardOpen == true; does nothing at all.

public static bool leaderboardOpen = false;
    private void leaderButton_Click(object sender, EventArgs e)
        {
            if (leaderboardOpen == false)
            {
                Leaderboard leaderboard = new Leaderboard();
                leaderboard.Show();
                leaderboardOpen = true;
            }
            else
            {
                Leaderboard leaderboard = new Leaderboard();
                 //Tried the below
                //leaderboard.Focus();
                //leaderboard.BringToFront();
                //leaderboard.TopMost = true;
                //leaderboard.Activate();
            }
        }
dwb
  • 475
  • 6
  • 31

3 Answers3

2

Instead of a bool, keep a reference to your instance of Leaderboard itself:

private Leaderboard leader = null;
private void leaderButton_Click(object sender, EventArgs e)
{
    if (leader == null || leader.IsDisposed)
    {
        leader = new Leaderboard();
        leader.FormClosed += (s2, e2) => { leader = null; };
        leader.Show();
    }
    else
    {
        if (leader.WindowState == FormWindowState.Minimized)
        {
            leader.WindowState = FormWindowState.Normal;
        }
        leader.BringToFront();
    }            
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • Thanks for the answer. I found another solution (posted). Is there a preferred solution between our 2 solutions? – dwb May 18 '22 at 21:37
  • 1
    It really doesn't matter; both will work just fine. The only time this approach would be "better" is if you had millions of Forms open since it could potentially take longer to find the instance of the form in the OpenForms collection. In reality, the number of forms will always be so small it doesn't matter... – Idle_Mind May 19 '22 at 01:00
2

Got it working with the below code.

private void leaderButton_Click(object sender, EventArgs e)
        {
            var form = Application.OpenForms.OfType<Leaderboard>().FirstOrDefault();

            if (form != null)
            {
                form.Activate();
            }
            else
            {
                new Leaderboard().Show();
            }
        }
dwb
  • 475
  • 6
  • 31
1

Here is a working code for me

public static bool leaderboardOpen = false;
public static Leaderboard? leaderboardForm = null;
private void leaderButton_Click(object sender, EventArgs e)
{
    if (!leaderboardOpened || leaderboardForm is null)
    {
        // Initialize Leaderboard form if it doesn't exist
        leaderboardForm = new Leaderboard();
        leaderboardForm.Show();
        leaderboardOpened = true;
    }
    else
    {
        // Focus on the form
        leaderboardForm.Focus();
    }
}

Make sure to make leaderboardOpened = false; if the Leaderboard form is closed.

Lanzy Erin
  • 33
  • 5