0

I'm attempting to create an application which requires login, I've been successful in getting the application to hide form 1 (main app) and load straight for form 2 (login) by initializing Application.Run(new Login());

How ever this can easily be bypassed by hitting the close application from the (Login)form. Which will then take you to form1 (MainApplication) I've tried defining on form close to exit the application but run into an issue where by successful logins will also close the entire application. Which is obviously not desired, can someone point me into the right direction of how to handle such an event?

Form1 (Main Application)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NewApplication1
{
    public partial class MainApp : Form
    {
        public MainApp()
        {
            InitializeComponent();
            Application.Run(new Login());
        }
    }
}

Form2 (Login)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace NewApplication1
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Click(object sender, EventArgs e)
        {
            try
            {
                string mySqlConnection = "datasource=xxxxx;port=3306;username='" + UserName_TXTBX.Text + "';password= '" + Password_TXTBX.Text + "'";
                MySqlConnection mySqlConn = new MySqlConnection(mySqlConnection);
                MySqlCommand cmd = new MySqlCommand();
                cmd.Connection = mySqlConn;
                MySqlDataAdapter myDataAdapter = new MySqlDataAdapter(cmd);
                MySqlCommandBuilder cb = new MySqlCommandBuilder(myDataAdapter);
                mySqlConn.Open();
                DataSet ds = new DataSet();
                mySqlConn.Close();
                Login.close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
        private static void close()
        {
          ActiveForm.Close();
        }
    }
}

Problem I have is when the form is closed by the "red 'x'" it will close the login form and continue to the MainApplication when I wish for it to exit the entire application.

H3ALY
  • 13
  • 8
  • Store the reason for the form closing (e.g. bool loginSuccess = true|false;) inside the form and react accordingly in the close event. – davidgiga1993 Jul 08 '15 at 12:37
  • 1
    You're not showing any code, so I'm just going to point you towards the code that works: see duplicate [How can I close a login form and show the main form without my application closing?](http://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi). Searching the web for "C# winforms show main form after login form close" yields plenty of results where you can see how it's generally done. – CodeCaster Jul 08 '15 at 12:39
  • When posting a question, you should put your current code, broken down to the simplest issue that you're asking about. Otherwise, it's really hard to suggest where to go. If I understand, it seems like a state problem... maybe you need to set a flag that only closes if login failed? – SpaceSteak Jul 08 '15 at 12:40

0 Answers0