# Help, How To Open A Form From Another Form



## char[] rager (Jun 12, 2010)

Here Is My Form1.cs:

```
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 Login
{
    public partial class Form1 : Form
    {
        int wrongCounter = 0;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "a")
            {
                wrongCounter++;

                if (wrongCounter == 2)
                {
                    Close();
                }

                textBox1.Clear();

                label1.Visible = true;
            }

            else
            {
                FormMain form = new FormMain();
                form.Show();

                Close();
            }
        }
    }
}
```

Here Is My FormMain.cs:

```
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 Login
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }
    }
}
```

As one can tell from my Form1 code, I am trying to open FormMain after the password is validated. However, how can I open FormMain from Form1 and close Form1 without closing FormMain? I ask this, because, even though the Close() function is called in Form1, it shuts both Form1 and FormMain down?


----------



## char[] rager (Jun 13, 2010)

Well, I just learned how to create tabs in Windows Forms, which is more effective, to me, than opening and managing more than one form.

However, if anyone was interested, I did cook up a ghetto solution to what I originally wanted, and that is whenever you want a button to open up a new form, you have to make that new form's object and then call the show() function for the new form, and this.Hide() to hide the original form from the viewer. Then, I had to write something to handle the new form closing, which had to include Application.OpenForms["form1"].Show() in order to show the invisible original form.


----------



## FordGT90Concept (Jun 13, 2010)

Basically you'd have to move it to Program.cs (this is pseudocode):


```
Form1 form1 = new Form1()
form1.ShowDialog();

Application.Run(new FormMain());
```
You use the Form1 to get necessary information and then pass the application off to FormMain.  Note that class "Program" is doing both of these things.  A form cannot close itself, only .Hide() itself.


----------



## char[] rager (Jun 13, 2010)

You know what, I did not think of putting any form control code in the Program.cs file. Thank You For that information FortGT90Concept!!!

However, for my applications now, I find that using tabs is better than opening and closing many different forms.


----------



## FordGT90Concept (Jun 13, 2010)

If you put the code on a UserControl, it's easier to move about.  You could have it display in a Form or in a TabControl.  All you have to do is add the UserControl to the container's "Controls" collection and then Controls["control_name"].Dock = DockStyle.Fill;

UserControls make the GUI components portable.


----------



## char[] rager (Jun 13, 2010)

I am going to learn about those UserControls now.

I do have a question about resource usage, even though I use blahblahblah.Dispose(), I notice that memory usage never decreases, it is as though the form is still there in memory?


----------



## FordGT90Concept (Jun 13, 2010)

C#.NET automatically takes care of disposal.  As far as I know, the most reliable way to dispose of something is to set the variable containing/referencing it to null.


----------



## char[] rager (Jun 13, 2010)

Memory usage goes up whenever I create the form and then I call the Dispose() function and set the object to null, and memory usage in the program never goes back down.


----------



## FordGT90Concept (Jun 13, 2010)

Windows might be reserving (heap) that memory for the application.  That doesn't necessarily mean Windows could reallocate it if it had to.


Ehm, if you aren't loading gobs of data, I wouldn't worry about memory management.


----------

