• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.
  • The forums have been upgraded with support for dark mode. By default it will follow the setting on your system/browser. You may override it by scrolling to the end of the page and clicking the gears icon.

Help, How To Open A Form From Another Form

char[] rager

New Member
Joined
Jun 9, 2010
Messages
310 (0.06/day)
Location
Massachusetts Institute of Technology, Computer Sc
System Name Obsidianight
Processor Intel Core i7 950 @ ~ 4 GHz
Motherboard Asus P6T Deluxe
Cooling Custom Liquid Cooling
Memory 6GB (3 x 2GB) Corsair XMS3 DDR3 Triple-Channel @ ~ 1.6 GHz (9-9-9-24-1T)
Video Card(s) Zotac AMP! GTX 580 @ 900 MHz Core / 1025 MHz Memory / 1800 MHz Shader
Storage 180 GB OCZ Vertex 2 SSD
Display(s) Sceptre 24 Inch (1920 x 1200) 2 MS Response
Case Corsair Obsidian 800D
Audio Device(s) Onboard
Power Supply 1 kW Antec TruePower Quattro
Software Microsoft Windows 7 Ultimate 64-Bit / Linux Mint 9 And Fedora 13 KDE Through VirtualBox
Here Is My Form1.cs:
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 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:
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 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?
 
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.
 
Basically you'd have to move it to Program.cs (this is pseudocode):

Code:
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.
 
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.
 
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.
 
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?
 
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.
 
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.
 
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.
 
Back
Top