Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6581403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:08:54+00:00 2026-05-25T16:08:54+00:00

I a form than I can open it also by pressing F1 and clicking

  • 0

I a form than I can open it also by pressing F1 and clicking so in this code first I check if the form has been opened or not.if was opened I just want to bring it to front. my problem is it wont bring in front by pressing F1 or clicking ToolStripMenuItem if i open it befor.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
  Form2 form2;
  form2 = new Form2();

  if (e.KeyCode.ToString() == "F1" && Application.OpenForms.OfType<Form2>().Count() <= 0)
    // the user pressed the F1 key
    form2.Show();
  else
  {
    form2.TopMost = true;
    form2.BringToFront();
    form2.Focus();
    form2.TopMost = false;
  }
}

private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
  Form2 form2;
  form2 = new Form2();
  if (Application.OpenForms.OfType<Form2>().Count() <= 0)
    form2.Show();
  else
  {
    form2.TopMost = true;
    form2.BringToFront();
    form2.Focus();
    form2.TopMost = false;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:08:55+00:00Added an answer on May 25, 2026 at 4:08 pm

    It doesn’t work because you forgot to call form2.Show() in the else clause. The code is wrong, you don’t want to create a new instance of Form2 if one already exists. Also, there’s a bug in Winforms that makes Application.OpenForms lose track of form instances.

    Best thing to do is to explicitly keep track of the lifetime of the form with its FormClosed event handler so you don’t have to find it back later:

        Form2 form2;
    
        private void showForm2() {
            if (form2 == null) {
                form2 = new Form2();
                form2.FormClosed += delegate { form2 = null; };
            }
            form2.Show();
            form2.Focus();
        }
    

    Call showForm2 from your event handlers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using jsf 1.2. When a particular jsp has more than one form with
Is it possible to open a form more than once? button1 form2.show Press button1
I have written a code to open a form if user enter value greater
I'm using the following code to open a new form (that is for user
I've been trying to figure this out, but it seems to be harder than
It has been a long question, so here is the summary first, I have
I have a page where there is currently more than one form. I used
I'm finding a way to count special character that form by more than one
I have a form, user enters some data then submits. Rather than a popup
Right now, I have more than 25 vertices that form a model. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.