c# hiding and showing forms


by madmike159
Tags: forms, hiding, showing
madmike159
madmike159 is offline
#1
Nov22-11, 10:41 AM
PF Gold
madmike159's Avatar
P: 370
In VB6 this was very easy. you just used

form1.hide
form2.show
In c# you have to do this

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 with out the data disappearing? Where should I create these instances?
Phys.Org News Partner Science news on Phys.org
Simplicity is key to co-operative robots
Chemical vapor deposition used to grow atomic layer materials on top of each other
Earliest ancestor of land herbivores discovered
Mark44
Mark44 is online now
#2
Nov22-11, 01:14 PM
Mentor
P: 20,948
Quote Quote by madmike159 View Post
In VB6 this was very easy. you just used

form1.hide
form2.show
In c# you have to do this

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:
form2 openForm2 = new form2(); //create a new instance
openForm2.show();
You don't give enough context for me to know what this code does.
this.hide();
Quote Quote by madmike159 View Post

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 with out the data disappearing? Where should I create these instances?
madmike159
madmike159 is offline
#3
Nov22-11, 01:30 PM
PF Gold
madmike159's Avatar
P: 370
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.

Mark44
Mark44 is online now
#4
Nov22-11, 02:10 PM
Mentor
P: 20,948

c# hiding and showing forms


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.

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.
AlephZero
AlephZero is offline
#5
Nov22-11, 02:29 PM
Engineering
Sci Advisor
HW Helper
Thanks
P: 6,342
The devil is in the detail (as with all programming).

show() doesn't work because that nice Mr Gates decided to call it Show().

Google is your friend. The first link I got for "c# show hide form" was
http://www.techotopia.com/index.php/...rms_in_C_Sharp
There are plenty more, if that doesn't answer your question.
madmike159
madmike159 is offline
#6
Nov22-11, 03:55 PM
PF Gold
madmike159's Avatar
P: 370
Ok let me try and explain again.

This is at the top of my Form1.cs file

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

       
 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

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)
rootX
rootX is offline
#7
Nov22-11, 05:16 PM
rootX's Avatar
P: 1,295
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();
}
Mark44
Mark44 is online now
#8
Nov22-11, 05:50 PM
Mentor
P: 20,948
Quote Quote by madmike159 View Post
Ok let me try and explain again.

This is at the top of my Form1.cs file

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

       
 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

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.
Quote Quote by madmike159 View Post

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
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.
madmike159
madmike159 is offline
#9
Nov23-11, 02:35 AM
PF Gold
madmike159's Avatar
P: 370
I get what you are saying about openTest. The problem is that I don't know where to define

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.
Mark44
Mark44 is online now
#10
Nov23-11, 09:13 AM
Mentor
P: 20,948
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.
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:
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.
madmike159
madmike159 is offline
#11
Nov23-11, 11:00 AM
PF Gold
madmike159's Avatar
P: 370
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.
Mark44
Mark44 is online now
#12
Nov23-11, 02:53 PM
Mentor
P: 20,948
Quote Quote by madmike159 View Post
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.
dkMft
dkMft is offline
#13
Dec15-11, 01:10 PM
P: 3
Keep track of the other form using a field in your main form:

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();
        }
    }
}


Register to reply

Related Discussions
Hiding behind the Internet. No anonymity. General Discussion 56
showing that a given set of vectors forms a basis Calculus & Beyond Homework 9
Hiding taskbar on a mac Computing & Technology 3
Hiding your ip adress through proxy servers? Computing & Technology 13