Link to home
Start Free TrialLog in
Avatar of ProWebNetworks
ProWebNetworks

asked on

Close one form and open another...

Application C# Windows Form Application

I have an application where an initial form opens up. This form offers the user a couple of different options, each option is supposed to open a form and close the originating form.

I have tried creating an instance of the second form in program.cs then opening it, and closing the initial form, but when I do the entire application ends.

I do not want an MDI setup - basically I want the program to end when the last form closes, not when the first form closes. I am having difficulties.
Avatar of ProWebNetworks
ProWebNetworks

ASKER

This is the current code of my program.cs

The name of the second form will be frmMainMenu, there will be many other forms in addition. Once they choose an option on Main Menu, it will then close and then open yet another form based on what they chose. All in all this app will have around 25 different forms, and typically only one form will be open at any one time. Once I close an originating form, there will be no need to keep it in memory, if I need to reopen it, then I will simply reopen it.

Thanks.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace SJRDB_C
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin());
 
            
        }
 
 
    }
}

Open in new window

in the frmLogin's Login button add these lines: (if access is granted)

frmLogin.Close();
frmMainMenu x =new frmMainMenu();
Simply, To close a form use Close() Method.
To run a form create an object of type that form, Also
Form.Show() and Form.Hide may help.
to exit a form use Application.Exit()
to run a form use Application.run(Object of type that form)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
 
namespace SJRDB_C
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmLogin());
            Application.Run(new frmMainMenu());\\ frmMainMenu will open after frmLogin been closed
 
            
        }
 
 
    }
}
 
Open in New Window Select All 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ProWebNetworks
ProWebNetworks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial