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. Problem with Form

Problem with Form

Scheduled Pinned Locked Moved C#
helpquestion
20 Posts 6 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.
  • L Lost User

    frmMenu is a new object, you never showed it, so it never closed. If you want to close the current form you can do the following...

    private void btn2Player_Click(object sender, EventArgs e)
    {
    frm2Players frm2P = new frm2Players();//new form
    frm2P.Show();//display new form
    this.Close();//Close the menu (current) form
    }

    Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

    L Offline
    L Offline
    Lim Yuxuan
    wrote on last edited by
    #5

    The menu form did closed but it seems that if the menu form is closed, the new form is closed together with it ... hmm maybe I should try MDI ?

    1 Reply Last reply
    0
    • L Lim Yuxuan

      Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :

      private void btn2Player_Click(object sender, EventArgs e)
      {
      frmMenu frmMenu = new frmMenu();// menu form
      frm2Players frm2P = new frm2Players();//new form
      frm2P.Show();//display new form
      frmMenu.Close();//Close the menu form
      }

      The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #6

      Lim Yuxuan wrote:

      frmMenu frmMenu = new frmMenu();// menu form

      Remove this line since it is not the current instance of menu form.

      Lim Yuxuan wrote:

      frmMenu.Close();//Close the menu form

      Replace this line by: this.Hide(); Do not use this.Close() since it will also dispose object frm2P and second form would be closed as well.

      It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

      modified on Monday, September 14, 2009 10:07 AM

      L 1 Reply Last reply
      0
      • L Lim Yuxuan

        Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :

        private void btn2Player_Click(object sender, EventArgs e)
        {
        frmMenu frmMenu = new frmMenu();// menu form
        frm2Players frm2P = new frm2Players();//new form
        frm2P.Show();//display new form
        frmMenu.Close();//Close the menu form
        }

        The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?

        M Offline
        M Offline
        musefan
        wrote on last edited by
        #7

        where are you calling your initial frmMenu form from? I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children. What you want to do is call frmMenu from wherever it is you call that now, and have that return a value (suggested through a property), when btn2Player is clicked then the property is set and the frmMenu is closed - the parent form/class can then use that property to determine what form to open next. something like...

        if(frmMenu.ShowDialog == DialogResult.OK)
        {
        switch(frmMenu.OptionProperty)
        {
        case 1://1 player
        frm1Player frm1P = new frm1Player();
        frm1P.Show();
        break;
        case 2://2 player
        frm2Players frm2P = new frm2Players();
        frm2P.Show();
        break;
        }

        }

        Life goes very fast. Tomorrow, today is already yesterday.

        L 1 Reply Last reply
        0
        • M musefan

          where are you calling your initial frmMenu form from? I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children. What you want to do is call frmMenu from wherever it is you call that now, and have that return a value (suggested through a property), when btn2Player is clicked then the property is set and the frmMenu is closed - the parent form/class can then use that property to determine what form to open next. something like...

          if(frmMenu.ShowDialog == DialogResult.OK)
          {
          switch(frmMenu.OptionProperty)
          {
          case 1://1 player
          frm1Player frm1P = new frm1Player();
          frm1P.Show();
          break;
          case 2://2 player
          frm2Players frm2P = new frm2Players();
          frm2P.Show();
          break;
          }

          }

          Life goes very fast. Tomorrow, today is already yesterday.

          L Offline
          L Offline
          Lim Yuxuan
          wrote on last edited by
          #8

          where are you calling your initial frmMenu form from?
          I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children.

          Yup. That is what happens when I tried to execute the .Close() method. I tried d@nish 's method and it worked the way I wanted it to be. Thanks anyway :)

          M 1 Reply Last reply
          0
          • D dan sh

            Lim Yuxuan wrote:

            frmMenu frmMenu = new frmMenu();// menu form

            Remove this line since it is not the current instance of menu form.

            Lim Yuxuan wrote:

            frmMenu.Close();//Close the menu form

            Replace this line by: this.Hide(); Do not use this.Close() since it will also dispose object frm2P and second form would be closed as well.

            It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

            modified on Monday, September 14, 2009 10:07 AM

            L Offline
            L Offline
            Lim Yuxuan
            wrote on last edited by
            #9

            Thanks it worked the way I wanted it to be.

            D 1 Reply Last reply
            0
            • L Lim Yuxuan

              Thanks it worked the way I wanted it to be.

              D Offline
              D Offline
              dan sh
              wrote on last edited by
              #10

              Make sure you close the menu form as well when you are closing the second form. You can access that using Application.OpenForms collection.

              It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

              1 Reply Last reply
              0
              • L Lim Yuxuan

                where are you calling your initial frmMenu form from?
                I think your problem is you are creating frm2Players from within frmMenu which means when that closes it will close all children.

                Yup. That is what happens when I tried to execute the .Close() method. I tried d@nish 's method and it worked the way I wanted it to be. Thanks anyway :)

                M Offline
                M Offline
                musefan
                wrote on last edited by
                #11

                No problem, but may I say that the apparent quick fix you have chosen may not be the best option. Will you need to display the frmMenu again at any point? Out of interest, I assume you are making a game or some sort? any particular reason to use windows forms?

                Life goes very fast. Tomorrow, today is already yesterday.

                D L 2 Replies Last reply
                0
                • M musefan

                  No problem, but may I say that the apparent quick fix you have chosen may not be the best option. Will you need to display the frmMenu again at any point? Out of interest, I assume you are making a game or some sort? any particular reason to use windows forms?

                  Life goes very fast. Tomorrow, today is already yesterday.

                  D Offline
                  D Offline
                  dan sh
                  wrote on last edited by
                  #12

                  Application.OpenForms collection would help out then.

                  It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                  M L 2 Replies Last reply
                  0
                  • L Lim Yuxuan

                    Yes. btn2Player calls a new form and after calling the new form, it will remove the old one (menu form)

                    H Offline
                    H Offline
                    Henry Minute
                    wrote on last edited by
                    #13

                    As ElliotA has said you are creating a new menu form and closing that, rather than the original one as you intend. To do what you want, change your code to something like:

                    private void btn2Player\_Click(object sender, EventArgs e)
                    {
                        // frmMenu frmMenu = new frmMenu();// menu form // <=============== Delete this line \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
                        frm2Players frm2P = new frm2Players();//new form
                        frm2P.Show();//display new form
                        // frmMenu.Close();//Close the menu form // <=============== Change this line \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
                        this.Close();//Close the menu form
                    }
                    

                    However, you said that the menu form is the first form in your application, so if you close it, you will close your whole application, including the new frm2Players that you have just created.

                    Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”

                    1 Reply Last reply
                    0
                    • D dan sh

                      Application.OpenForms collection would help out then.

                      It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                      M Offline
                      M Offline
                      musefan
                      wrote on last edited by
                      #14

                      Yeah maybe, I think it depends on the usage of the menu form, if its a 'main menu' kind of form then it should be re-opened when the game form is done, unless a complete quit selection of some sort is made... though, you could of course use your method to show the menu form again. I guess it's just preference at the minute (maybe i'll think of a good reason either way later ;) )

                      Life goes very fast. Tomorrow, today is already yesterday.

                      1 Reply Last reply
                      0
                      • M musefan

                        No problem, but may I say that the apparent quick fix you have chosen may not be the best option. Will you need to display the frmMenu again at any point? Out of interest, I assume you are making a game or some sort? any particular reason to use windows forms?

                        Life goes very fast. Tomorrow, today is already yesterday.

                        L Offline
                        L Offline
                        Lim Yuxuan
                        wrote on last edited by
                        #15

                        Yup Its a tic-tac-toe. I am using labels for the "O" and "X". But. I think I will use MDI instead because I could not figure out where to put the

                        frmMenu.Show();

                        code to show the menu form once i close the game form (I could not find any event handler that triggers when you close the form). EDIT:

                        Application.OpenForms collection would help out then.

                        oh wait ...

                        modified on Monday, September 14, 2009 7:26 PM

                        D 1 Reply Last reply
                        0
                        • D dan sh

                          Application.OpenForms collection would help out then.

                          It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                          L Offline
                          L Offline
                          Lim Yuxuan
                          wrote on last edited by
                          #16

                          Erm.. where should I put the

                          Application.OpenForms[0].Show();

                          code ? I cant find an event handler that triggers when you closes a form.

                          1 Reply Last reply
                          0
                          • L Lim Yuxuan

                            Yup Its a tic-tac-toe. I am using labels for the "O" and "X". But. I think I will use MDI instead because I could not figure out where to put the

                            frmMenu.Show();

                            code to show the menu form once i close the game form (I could not find any event handler that triggers when you close the form). EDIT:

                            Application.OpenForms collection would help out then.

                            oh wait ...

                            modified on Monday, September 14, 2009 7:26 PM

                            D Offline
                            D Offline
                            dan sh
                            wrote on last edited by
                            #17

                            There is a Form_Closing event. You can make use of that.

                            It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                            L 1 Reply Last reply
                            0
                            • D dan sh

                              There is a Form_Closing event. You can make use of that.

                              It's not necessary to be so stupid, either, but people manage it. - Christian Graus, 2009 AD

                              L Offline
                              L Offline
                              Lim Yuxuan
                              wrote on last edited by
                              #18

                              thanks

                              1 Reply Last reply
                              0
                              • L Lim Yuxuan

                                Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :

                                private void btn2Player_Click(object sender, EventArgs e)
                                {
                                frmMenu frmMenu = new frmMenu();// menu form
                                frm2Players frm2P = new frm2Players();//new form
                                frm2P.Show();//display new form
                                frmMenu.Close();//Close the menu form
                                }

                                The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?

                                C Offline
                                C Offline
                                carlecomm
                                wrote on last edited by
                                #19

                                Hi, you can't close it. because 'frm2players' is one child form of the 'frmMenu'; Give you two : 1) hide the 'frmMenu'; use 'frmMenu.visible = false;' or 'frmMenu.Hide();' 2) Create one Thread to create 'frm2Players'; /********************/ 'Thread td = new thread(method);' // method is 'create frm2player' td.start(); /********************/ note: thread is 'light', so can't have Resources as 'Main thread'.

                                modified on Tuesday, September 22, 2009 2:13 AM

                                1 Reply Last reply
                                0
                                • L Lim Yuxuan

                                  Hi guys,I have a program that starts with a menu form. It will create a new form and remove the menu form after the user pressed a certain button. So I tried the following method :

                                  private void btn2Player_Click(object sender, EventArgs e)
                                  {
                                  frmMenu frmMenu = new frmMenu();// menu form
                                  frm2Players frm2P = new frm2Players();//new form
                                  frm2P.Show();//display new form
                                  frmMenu.Close();//Close the menu form
                                  }

                                  The new form that it is supposed to create shows up but strangely the menu form is still there. What should I do ?

                                  C Offline
                                  C Offline
                                  carlecomm
                                  wrote on last edited by
                                  #20

                                  you can't close it. because 'frm2players' is one child form of the 'frmMenu'; Give you two : 1) hide the 'frmMenu'; use 'frmMenu.visible = false;' or 'frmMenu.Hide();' 2) Create one Thread to create 'frm2Players'; /********************/ 'Thread td = new thread(method);' // method is 'create frm2player' td.start(); /********************/ note: thread is 'light', so can't have Resources as 'Main thread'.

                                  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