Hide and Show of Form in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jetean
    New Member
    • Feb 2008
    • 33

    Hide and Show of Form in C#

    Hi:
    If I have 2 forms. Form1 have a timer that increase a counter and display in a textbox. If I press a button Form1 will be hide but still in process in the background, and Form2 will be displayed. Then If I press a button in Form2, Form 2 will be hide while Form1 will be display again ( with the latest counter value).

    How can i do it?

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Open the docs for the Form class and browse through all the methods in it. The one you want should be easy to spot.

    P.S Refuse to write any code if you don't have either the docs or intellisense.

    Comment

    • Jetean
      New Member
      • Feb 2008
      • 33

      #3
      I tried in Form2 by creating a new instance.

      Like this: Form1 newForm= new Form1();
      newForm.show();


      But that it the new instance, not the one that run on background..

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Jetean
        I tried in Form2 by creating a new instance.

        Like this: Form1 newForm= new Form1();
        newForm.show();


        But that it the new instance, not the one that run on background..
        Use the Hide and Show methods on those same Forms that you have. Using the new keyword will create a, well, new (different) form.

        Comment

        • tezza98
          New Member
          • Mar 2007
          • 38

          #5
          Form1.visible = false
          form2.visible = true

          Comment

          • IanWright
            New Member
            • Jan 2008
            • 179

            #6
            Originally posted by Jetean
            I tried in Form2 by creating a new instance.

            Like this: Form1 newForm= new Form1();
            newForm.show();


            But that it the new instance, not the one that run on background..
            Indeed that will be a new instance... So what you need is the first instance? So you need to a reference to that on Form2...

            So how about?

            Code:
            // within Form2
            
            Form1 form1;
            
            Initialize(Form1 form1)
            {
               this.form1 = form1;
            }
            
            public override void Show()
            {
               this.form1.Hide();
               base.Show();
            }
            
            public void ButtonClick(object sender, EventArgs e)
            {
               this.form1.Hide();
            }
            Or even better you could have an event handler on Form1, which makes it hide/show itself depening on the result...

            Code:
            // on Form1
            
            public ShowHideToggle(object sender, bool show)
            {
              if(show)
                  this.Show();
               else
                  this.Hide();
            }
            
            
            // on Form2
            public event EventHandler<bool> ShowHide;
            
            Initialize(Form1 form1)
            {
                this.ShowHide += form1.ShowHideToggle;
            }
            
            public override void Show()
            {
               if(ShowHide != null)
                  ShowHide(false);
            
               base.Show();
            }
            
            public void ButtonPressed(object sender, EventArgs e)
            {
               if(ShowHide != null)
                  ShowHide(true);
            }
            This then allows Form2 to not need to know about Form1 once it has an EventHandler set up...

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Originally posted by Jetean
              I tried in Form2 by creating a new instance.

              Like this: Form1 newForm= new Form1();
              newForm.show();


              But that it the new instance, not the one that run on background..
              The computer does only what you tell it - and EXACTLY what you tell it to do.

              You told it to create a *new* Form1 so that's what it did. It created a *new* instance of a Form1 object, and thus it was not the hidden instance of the earlier Form1. Those two Form1's might look the same, but they are entirely different objects.

              Do some searching about how to communicate between classes. Shift your thinking from making new items, to having methods inside each class that perform the functions you need. Maybe something like a method in Form1 that directs the form to hide itself, and one to show itself. Same with Form2. Then work out a way for Form2 to tell Form1 to perform the method you want.

              A little less of 'A' directly affecting 'B'. A little more of 'A' telling 'B' in what manner to take care of itself.

              If your Form2 class always knows how to take care of itself then you only have to write a method one time for "update", "show", "hide", "load", "resize", whatever. But if you try to make Form1 responsible for Form2, then you have to keep everything very synchronized. Form3 will have to duplicate all the control you put in Form1. If you make a change to Form2, you have to update Form1 and Form3 with identical changes.

              Comment

              • Jetean
                New Member
                • Feb 2008
                • 33

                #8
                Originally posted by IanWright
                Indeed that will be a new instance... So what you need is the first instance? So you need to a reference to that on Form2...

                So how about?

                Code:
                // within Form2
                
                Form1 form1;
                
                Initialize(Form1 form1)
                {
                   this.form1 = form1;
                }
                
                public override void Show()
                {
                   this.form1.Hide();
                   base.Show();
                }
                
                public void ButtonClick(object sender, EventArgs e)
                {
                   this.form1.Hide();
                }
                Or even better you could have an event handler on Form1, which makes it hide/show itself depening on the result...

                Code:
                // on Form1
                
                public ShowHideToggle(object sender, bool show)
                {
                  if(show)
                      this.Show();
                   else
                      this.Hide();
                }
                
                
                // on Form2
                public event EventHandler<bool> ShowHide;
                
                Initialize(Form1 form1)
                {
                    this.ShowHide += form1.ShowHideToggle;
                }
                
                public override void Show()
                {
                   if(ShowHide != null)
                      ShowHide(false);
                
                   base.Show();
                }
                
                public void ButtonPressed(object sender, EventArgs e)
                {
                   if(ShowHide != null)
                      ShowHide(true);
                }
                This then allows Form2 to not need to know about Form1 once it has an EventHandler set up...
                How to do this ? I got error using your code
                Form1 form1;

                Initialize(Form 1 form1)
                {
                this.form1 = form1;
                }

                Comment

                • joedeene
                  Contributor
                  • Jul 2008
                  • 579

                  #9
                  why not just use the .hide() and .show() methods, once you have your referenced form ?

                  joedeene

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by joedeene
                    why not just use the .hide() and .show() methods, once you have your referenced form ?

                    joedeene
                    I'm afraid this is turning into one of those threads ...

                    Comment

                    • IanWright
                      New Member
                      • Jan 2008
                      • 179

                      #11
                      Originally posted by Jetean
                      How to do this ? I got error using your code
                      Form1 form1;

                      Initialize(Form 1 form1)
                      {
                      this.form1 = form1;
                      }
                      Within Form1, when you create Form2 you need to be doing

                      Code:
                      Form2 f2 = new Form2();
                      f2.Initialize(this);

                      Comment

                      • joedeene
                        Contributor
                        • Jul 2008
                        • 579

                        #12
                        Originally posted by r035198x
                        I'm afraid this is turning into one of those threads ...
                        ..one of those threads?

                        joedeene

                        Comment

                        • Frinavale
                          Recognized Expert Expert
                          • Oct 2006
                          • 9749

                          #13
                          Originally posted by joedeene
                          why not just use the .hide() and .show() methods, once you have your referenced form ?

                          joedeene
                          I was wondering the same thing myself......

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by joedeene
                            ..one of those threads?

                            joedeene
                            ... where the solution has already been given but the thread rumbles on because the OP doesn't bother reading(*) about the easy solution.

                            (*) Reading : An extinct art that was used to obtain information.

                            Comment

                            • dennijr
                              New Member
                              • May 2007
                              • 17

                              #15
                              i tried a bunch of things using given code and could not get it to work either...

                              Comment

                              Working...