How can I properly manage instances of forms when hiding and showing in C#?

  • C#
  • Thread starter madmike159
  • Start date
  • Tags
    Forms
In summary, the conversation discusses the difference between hiding and showing forms in VB6 and C#, and how to do so in C#. It also mentions the problem of creating new instances of forms and causing a stack overflow. The solution is to create instances of the forms at the top of the form code and only keep the show and hide functions in the button click event. The conversation also includes a code example and mentions the importance of understanding the difference between a class and an object in programming.
  • #1
madmike159
Gold Member
371
0
In VB6 this was very easy. you just used

Code:
form1.hide
form2.show

In c# you have to do this

Code:
form2 openForm2 = new form2(); //create a new instance
form2.show();
this.hide();

When I put this in a button it worked, but every time I changed forms it reset the form values to default (e.g. text boxes would become blank etc).
I was told this create a new instance every time, which was then lost so it would create a new instance every time with default values. So it put the form2 openForm2 = new form2(); at the top of the form code and have only kept the form2.show(); and this.hide(); in the button click.

The problem I now have is that because I need to declare an instance of each of my three forms I am causing a stack overflow.

How can I show and hide forms without the data disappearing? Where should I create these instances?
 
Technology news on Phys.org
  • #2
madmike159 said:
In VB6 this was very easy. you just used

Code:
form1.hide
form2.show

In c# you have to do this

Code:
form2 openForm2 = new form2(); //create a new instance
form2.show();
this.hide();
This code looks wrong to me. Apparently form2 is some type (i.e., class) that you have defined, and openForm2 is an instance of that class.

To show openForm2 (not form2), do this:
Code:
form2 openForm2 = new form2(); //create a new instance
openForm2.show();

You don't give enough context for me to know what this code does.
Code:
this.hide();
madmike159 said:
When I put this in a button it worked, but every time I changed forms it reset the form values to default (e.g. text boxes would become blank etc).
I was told this create a new instance every time, which was then lost so it would create a new instance every time with default values. So it put the form2 openForm2 = new form2(); at the top of the form code and have only kept the form2.show(); and this.hide(); in the button click.

The problem I now have is that because I need to declare an instance of each of my three forms I am causing a stack overflow.

How can I show and hide forms without the data disappearing? Where should I create these instances?
 
  • #3
Well when I try form2.show(); or form2.hide(); it says form2 does not have those argument or values.

So I do form2.

and show and hide don't appear in the list.

How would you hide and show a form? I created these forms using the add item option in visual studio, and the code op show another form needs to go into a button on a different form.
 
  • #4
From your C# code, it appears that form2 is the name of the class and openForm2 is the name of an instance of the form2 class.

I think you are confused between the difference between a class and an object (an instance of a class).

Try something like this.

Code:
using System.Windows.Forms;
.
.
.
Form myForm = new Form(); // Create a new instance of the Form class.
myForm.Show(); // Show the form.
.
.
.
myForm.Hide(); // Hide the form.
 
  • #5
  • #6
Ok let me try and explain again.

This is at the top of my Form1.cs file

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SRV_GUI
{
    public partial class SRSV : Form
    {

        double oldGPSNorth = 0;
        double oldGPSEast = 0;
        int oldBearing = 0;
        int oldBatteryLevel = 0;
        
        public SRSV()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

Then in a button I have

Code:
 private void button2_Click(object sender, EventArgs e)
 {
      //This button opens the Test form and hides the main form
      Test openTest = new Test();
      openTest.Show();
      this.Hide();
}

Which refers to this form

Code:
namespace SRV_GUI
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }

I want to be able to hide the first form (which was called form1 by default, but I changed to SRSV), and to show Test (originally called form2).

When I have Test openTest = new Test(); in a button it causes the forms to have their default values when they are shown again since I keep creating different instances of them.

I can do Test.Show() as this gives errors (Error 1 An object reference is required for the non-static field, method, or property) or Form2.Show() (Form2 doesn't exist in the current context)
 
  • #7
try:Test openTest = new Test();
private void button2_Click(object sender, EventArgs e)
{
//This button opens the Test form and hides the main
openTest.Show();
this.Hide();
}
 
  • #8
madmike159 said:
Ok let me try and explain again.

This is at the top of my Form1.cs file

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SRV_GUI
{
    public partial class SRSV : Form
    {

        double oldGPSNorth = 0;
        double oldGPSEast = 0;
        int oldBearing = 0;
        int oldBatteryLevel = 0;
        
        public SRSV()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

Then in a button I have

Code:
 private void button2_Click(object sender, EventArgs e)
 {
      //This button opens the Test form and hides the main form
      Test openTest = new Test();
      openTest.Show();
      this.Hide();
}

Which refers to this form

Code:
namespace SRV_GUI
{
    public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
        }

I want to be able to hide the first form (which was called form1 by default, but I changed to SRSV), and to show Test (originally called form2).

When I have Test openTest = new Test(); in a button it causes the forms to have their default values when they are shown again since I keep creating different instances of them.
Test is the name of the class. openTest is an instance of the Test class.
madmike159 said:
I can do Test.Show() as this gives errors (Error 1 An object reference is required for the non-static field, method, or property) or Form2.Show() (Form2 doesn't exist in the current context)
You don't want to call Show() on the class - call it on the instance you created, openTest, as in
Code:
openTest.Show();

You really need to understand the difference between a class (e.g., Test) and an instance of the class (e.g., openTest). Once you create an instance of the class, then you can call the class methods. It is possible to call some methods on the class itself, but I don't think you're at the stage yet to understand this fine point.
 
  • #9
I get what you are saying about openTest. The problem is that I don't know where to define

Code:
Test openTest = new Test();

Because if I do it in the button click code it works, but the form labels and text boxes go back to their default values every time, which is irritating.

I tried doing this at the beginning of each partial class bit, but this gave me stack overflow errors.
 
  • #10
If you define openTest inside the body of a method, as you showed in post #6, the scope of openTest is just the method in which it is defined. In other words, it is not visible outside your Click event handler.
Code:
private void button2_Click(object sender, EventArgs e)
 {
      //This button opens the Test form and hides the main form
      Test openTest = new Test();
      openTest.Show();
      this.Hide();
}
The net effect of this is, after the Click event handler is finished, then openTest ceases to exist. This is why the form labels revert to their values in the other form.

One possible fix would be to use the OwnedForms property on your form. This is an array of Form objects that is owned by the top-level Form. After you have initialized the 0-element of this array with the openTest form, you could do something like this:
Code:
this.Hide(); // Hide the SRSV form. 
this.OwnedForms[0].Show(); // Show the openTest form

I haven't tried this, so I can't guarantee it will work for you.

BTW, you ought to choose some better names for your forms. openTest gives no clue that it is the name of a form. A better name would be testForm.
 
  • #11
Ok thanks, I will try this.

I rarely program something where other people will have access to the source code, which is often why I do things that are unclear and make stupid mistakes.
 
  • #12
madmike159 said:
I rarely program something where other people will have access to the source code, which is often why I do things that are unclear and make stupid mistakes.
Most everyone makes dumb mistakes from time to time, but you make life much more difficult than it needs to be when your code is unclear. If your variable names aren't chosen so that their purpose is clear, anyone reading your code, including yourself, will have a harder time trying to figure it out.
 
  • #13
Keep track of the other form using a field in your main form:

Code:
namespace SRV_GUI
{
    public partial class SRSV : Form
    {
        private TestForm _otherForm; // instance variable (field) in your main form

        //... other stuff happens
        public SRSV()
        {
            InitializeComponent();
            _otherForm = new TestForm(); // initialize once in constructor
            _otherForm.Hide();
        }

        
        private void button2_Click(object sender, EventArgs e)
        {
            // only do toggle logic in the button click event handler.
            _otherForm.Show();
            this.Hide();
        }
    }
}
 

What is the purpose of hiding and showing forms in C#?

Hiding and showing forms in C# is a way to manage the visibility of different forms in a user interface. This allows for a more organized and dynamic display of information for the user.

How do you hide a form in C#?

To hide a form in C#, you can use the Hide() method. This will make the form invisible to the user, but it will still exist in the background and can be shown again later.

How do you show a hidden form in C#?

To show a hidden form in C#, you can use the Show() method. This will make the form visible to the user again.

Can you hide a form without closing it in C#?

Yes, you can hide a form without closing it in C#. This can be useful for forms that need to remain open in the background, but are not currently needed by the user.

What is the difference between hiding and closing a form in C#?

Hiding a form makes it invisible to the user, but it still exists in the background and can be shown again. Closing a form, on the other hand, completely removes it from the user interface. This means that the form and all of its associated resources will be freed from memory.

Similar threads

  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
4
Views
906
  • Quantum Interpretations and Foundations
Replies
25
Views
997
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
2
Replies
49
Views
10K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
5
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
264
Back
Top