Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Close one form and open another...

Posted on 2009-04-08
4
Medium Priority
?
786 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:ProWebNetworks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 1

Author Comment

by:ProWebNetworks
ID: 24101291
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

0
 
LVL 6

Expert Comment

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

frmLogin.Close();
frmMainMenu x =new frmMainMenu();
0
 
LVL 6

Expert Comment

by:SaedSalman
ID: 24103112
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

0
 
LVL 1

Accepted Solution

by:
ProWebNetworks earned 0 total points
ID: 24110853
I added a static class for handling form calling called FormHandler. As you can see in the code below I called:

FormHandler.Open_frmLogin();

Then I used the Application.Run();

This way I could have all forms within the application close and the application would still continue to run, allowing me to open a new form. With the example you showed above, the main menu would have about 10 options on it, allowing the user to open any of 10 different forms.

At that point I would run into a problem adding the Application.Run method you have above. I would then be required to hide frmMainMenu as opposed to closing it out completely. I know it should not be required to close it, however I prefer that, it seems cleaner.

Can you give your opinion as to my solution here? It does work - but I am not sure if I am following proper procedure for how I am calling multiple forms.

Thanks.
namespace SJRDB_C
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            FormHandler.Open_frmLogin();
            Application.Run();
        }
    }
}

Open in new window

0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question