Victor Bjelkholm
Victor Bjelkholm

Reputation: 2216

Close form without exit application

I'm currently working on a small project and would like some help on it.

I have 2 forms, the first is a login window and the second will be the main program. The problem I have is that when I close form1 with this.Close() it is exiting the whole program.

I have a feeling that I need to use threading or something like that but I can't find the right resource for my problem to be solved.

Thanks.

Upvotes: 10

Views: 34735

Answers (6)

Basheer AL-MOMANI
Basheer AL-MOMANI

Reputation: 15327

whats your opinion to create something like that

tray Icon

its called tray Icon

so user can close all forms and the app still running,, and user can go back to app any time

lets start coding (note this is WPF Application)

private NotifyIcon m_notifyIcon;
private ContextMenuStrip m_contextMenu;
private bool _ForceClose;

public MainWindow()
{
     InitializeComponent();
     CreateNotifyIcon();

}

private void CreateNotifyIcon()
{
    m_contextMenu = new ContextMenuStrip();

    ToolStripMenuItem mI1 = new ToolStripMenuItem { Text = "Open" };
    mI1.Click += (sender, args) => Maximize();
    ToolStripMenuItem mI2 = new ToolStripMenuItem { Text = "Exit" };
    mI2.Click += (sender, args) => EndApplication();
    m_contextMenu.Items.Add(mI1);
    m_contextMenu.Items.Add(mI2);
    //Initalize Notify Icon
    m_notifyIcon = new NotifyIcon
    {
        Text = "Application Title",
        Icon = new Icon("Icon.ico"),
        //Associate the contextmenustrip with notify icon
        ContextMenuStrip = m_contextMenu,
        Visible = true
    };
}

and we have these functions

protected void Minimize()
{
    Hide();
}

protected void Maximize()
{
    Show();
    this.WindowState =WindowState.Normal;
}

protected void EndApplication()
{
    Minimize();
    Thread.Sleep(1000);
    _ForceClose = true;
    WindowState = WindowState.Normal;
    this.Close();
    Environment.Exit(0);
}

and in Window Closing event listener (don't forget to add it)

private void Window_Closing(object sender, CancelEventArgs e)
{
    if (_ForceClose == false)
    {
        e.Cancel = true;
        Minimize();
    }
}

that's it, hope it help you

Upvotes: 1

Khadaji
Khadaji

Reputation: 2167

Open Program.cs - you can do quite a bit before the call to Application run, including showing your login information. Here is a sample from one of my projects. It tries to find the database connection. If it can't, it opens up a wizard and either connects to access or mssql. If the open is good it shows a spalsh screen and finally runs the application, else it closes.

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
DialogResult LclResult;

EESDatabasePersistentData LclPersist;
LclPersist = new EESDatabasePersistentData();
string LclDataType;
string LclDatabase;
string LclServer;
string Password;
string UserID;
if (!LclPersist.GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password))
{
        // Run the connection wizard
        GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password);
}


if (LclDataType == "ACCESS")
        InitDataAccess(LclDatabase);
else if (LclDataType == "SQLSERVER")
        InitDataSQL(LclServer, LclDatabase, UserID, Password);
if (OpenGood)
{
        if (!System.Diagnostics.Debugger.IsAttached)
        {
                FormSplash.Instance.SetupVersion();
                ///////////////////////////////////
                // If we don't call do events 
                // splash delays loading.
                Application.DoEvents();
                FormSplash.Instance.Show();
        }
        Application.DoEvents();

        Application.Run(new FormMentorMain());
}
else
        Application.Exit();

Upvotes: 0

Mike Webb
Mike Webb

Reputation: 8993

Program.cs is where your main function is. If you created the project as a Windows App with Visual Studio then there will be a function in main that runs the form that opens when you start the program. Just get the login info from the login form in main and then call the second window. Here is an example:

[STAThread]
private static void Main(string[] args)
{
    //there's some other code here that initializes the program

    //Starts the first form (login form)
    Application.Run(new form1());

    //Get the login info here somehow. Maybe save in public members of form1 or
    // in a global utilities or global user class of some kind

    //Run the main program
    Application.Run(new mainProgramForm());
}

EDIT: FORGOT SOMETHING

I forgot to mention that if you do get members from the login form you will have to instantiate it first. This is not a good technique, and I recommend doing the Global user class idea over this, but I have a program that requires this method, and since I mentioned it, here is the example:

private static void Main(string[] args)
{
    //there's some other code here that initializes the program

    //Instead of running a new form here, first create it and then run it
    Form1 form1 = new Form1();    //Creates the form
    Application.Run(form1);       //Runs the form. Program.cs will continue after the form closes

    //Get the login info
    string username = form1.Username;
    string password = form1.Password;

    //Get rid of form1 if you choose
    form1.Dispose();

    //Validate the user info

    //Run the main program
    Application.Run(new mainProgramForm());
}

Upvotes: 2

Blam
Blam

Reputation: 2965

Can't you change your program.cs so that it runs the main form, and in the launch of the main form it creates and shows a login form and then hides itself (waiting for the login to happen to show itself)?

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564333

If you're using WPF, you can set Application.MainWindow to your second "main" window, prior to closing your login form.

Upvotes: 3

user159088
user159088

Reputation:

You could hide the first form instead of closing it:

this.Hide();
Form2 form2 = new Form2();
form2.Show();

Upvotes: 8

Related Questions