Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to launch a form only once

How to launch a form only once

Scheduled Pinned Locked Moved C#
csharphelpdatabasevisual-studiotutorial
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Ismael_1999
    wrote on last edited by
    #1

    Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:

    private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
    {
    if (FormEfetivas == null)
    {
    FormEfetivas = new Frm_Saida();
    FormEfetivas.MdiParent = this;
    FormEfetivas.Text = "Saídas Efetivas";
    FormEfetivas.Tag = "1";
    FormEfetivas.Show();
    }
    FormEfetivas.Activate();
    }
    private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
    {
    if (FormPrevisao == null)
    {
    FormPrevisao = new Frm_Saida();
    FormPrevisao.MdiParent = this;
    FormPrevisao.Text = "Previsão de Saídas";
    FormPrevisao.Tag = "0";
    FormPrevisao.Show();
    }

    Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.

    OriginalGriffO L Richard DeemingR 3 Replies Last reply
    0
    • I Ismael_1999

      Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:

      private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
      {
      if (FormEfetivas == null)
      {
      FormEfetivas = new Frm_Saida();
      FormEfetivas.MdiParent = this;
      FormEfetivas.Text = "Saídas Efetivas";
      FormEfetivas.Tag = "1";
      FormEfetivas.Show();
      }
      FormEfetivas.Activate();
      }
      private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
      {
      if (FormPrevisao == null)
      {
      FormPrevisao = new Frm_Saida();
      FormPrevisao.MdiParent = this;
      FormPrevisao.Text = "Previsão de Saídas";
      FormPrevisao.Tag = "0";
      FormPrevisao.Show();
      }

      Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Add a handler to the Form.Closed event when you create the instance, and in the handler set the appropriate variable (for example FormEfetivas to null. The existing code will then generate a new instance when they try next time.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      I 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Add a handler to the Form.Closed event when you create the instance, and in the handler set the appropriate variable (for example FormEfetivas to null. The existing code will then generate a new instance when they try next time.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

        I Offline
        I Offline
        Ismael_1999
        wrote on last edited by
        #3

        Hi, OriginalGriff. I didn't follow you. Could you explain better? Or give me an example? Thanks.

        OriginalGriffO 1 Reply Last reply
        0
        • I Ismael_1999

          Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:

          private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
          {
          if (FormEfetivas == null)
          {
          FormEfetivas = new Frm_Saida();
          FormEfetivas.MdiParent = this;
          FormEfetivas.Text = "Saídas Efetivas";
          FormEfetivas.Tag = "1";
          FormEfetivas.Show();
          }
          FormEfetivas.Activate();
          }
          private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
          {
          if (FormPrevisao == null)
          {
          FormPrevisao = new Frm_Saida();
          FormPrevisao.MdiParent = this;
          FormPrevisao.Text = "Previsão de Saídas";
          FormPrevisao.Tag = "0";
          FormPrevisao.Show();
          }

          Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You loop through the existing Windows / forms before deciding to create an instance. [Get List of all Open Forms in Windows Application](https://www.c-sharpcorner.com/blogs/get-list-of-all-open-forms-in-windows-application1) Or, use a Mutex. [mutex - How to run one instance of a c# WinForm application? - Stack Overflow](https://stackoverflow.com/questions/12340043/how-to-run-one-instance-of-a-c-sharp-winform-application)

          It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food

          1 Reply Last reply
          0
          • I Ismael_1999

            Hi, OriginalGriff. I didn't follow you. Could you explain better? Or give me an example? Thanks.

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            What part doesn't make sense?

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            I 1 Reply Last reply
            0
            • I Ismael_1999

              Hi, folks. I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB). I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:

              private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
              {
              if (FormEfetivas == null)
              {
              FormEfetivas = new Frm_Saida();
              FormEfetivas.MdiParent = this;
              FormEfetivas.Text = "Saídas Efetivas";
              FormEfetivas.Tag = "1";
              FormEfetivas.Show();
              }
              FormEfetivas.Activate();
              }
              private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
              {
              if (FormPrevisao == null)
              {
              FormPrevisao = new Frm_Saida();
              FormPrevisao.MdiParent = this;
              FormPrevisao.Text = "Previsão de Saídas";
              FormPrevisao.Tag = "0";
              FormPrevisao.Show();
              }

              Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form. What am I forgetting here? Can anybody help me? Thanks.

              Richard DeemingR Offline
              Richard DeemingR Offline
              Richard Deeming
              wrote on last edited by
              #6

              Simple enough:

              private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
              {
              var theForm = Application.OpenForms.OfType<Frm_Saida>().FirstOrDefault();
              if (theForm is null)
              {
              theForm = new Frm_Saida();
              theForm.MdiParent = this;
              theForm.Text = "Saídas Efetivas";
              theForm.Tag = "1";
              theForm.Show();
              }

              theForm.Activate();
              

              }


              "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

              "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

              1 Reply Last reply
              0
              • OriginalGriffO OriginalGriff

                What part doesn't make sense?

                "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                I Offline
                I Offline
                Ismael_1999
                wrote on last edited by
                #7

                It's not a matter of making sense. I understand what you wrote, but I'm confused about how to do it. Could you explain it? Thanks.

                OriginalGriffO 1 Reply Last reply
                0
                • I Ismael_1999

                  It's not a matter of making sense. I understand what you wrote, but I'm confused about how to do it. Could you explain it? Thanks.

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  Explain what? I have no idea which bits you are capable of doing for yourself! So ... you know how to add an event handler, yes?

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups