1

After 6 hours of analyzing and following this POST solution, my loginform frmLog still wont close. I created two forms in Microsoft visual studio C# window form application. My problem is even if I use different methods and tried everything the login form won’t close or hide. This is very annoying and cause me so much time just on this problem. Please help me.

this is my code in Program.cs where the start up form is initialize.

 public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            frmLog fLogin = new frmLog();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new frmMain());
            }
            else
            {
                Application.Exit();
            }
        }

And this is my login form code which shows the main form frmMain if the user and password is true they are actually connected to database.

 private void btnSubmit_Click(object sender, EventArgs e)
        {

                Mylibrary a = new Mylibrary("localhost", "root", "", "cashieringdb");
                string user = txtLogin.Text;
                string pass = txtPassword.Text;
                string query = "SELECT * FROM register WHERE username='" + user + "' AND password=MD5('" + pass + "')";
                int result = a.Count(query);
                if (result == 1)
                {
                    LOGIN_USER = txtLogin.Text;
                    frmMain main = new frmMain();
                    main.Show();
                    this.close
                }
                else
                {
                    MessageBox.Show("Login Failed! Try Again");
                    txtLogin.Text = "";
                    txtPassword.Text = "";
                }

        }

Please help me solve this problem.

Community
  • 1
  • 1
user2262382
  • 335
  • 2
  • 5
  • 13
  • You can Directly call frmMain form frmLogin right.? what you are trying to achieve with fLogin.ShowDialog() == DialogResult.OK – Akshay Joy Apr 15 '13 at 10:07
  • You are vulnerable to SQL Injection using this code. – Bas Apr 15 '13 at 10:09
  • Yeah I tried that too I initialize login form `frmLog` to the main form and the problem is if the main for will show there is another login form that will show and everything keeps repeating. – user2262382 Apr 15 '13 at 10:10
  • no worry for sql injection I just want my freaking login form to be close. I am new to database so sql injection I will study it later after I finish this program. – user2262382 Apr 15 '13 at 10:11

7 Answers7

2

use this

  static void Main()


{
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmLog ());

    }

inside frmlog

use

this.hide();
new frmMain().ShowDialog();
Akshay Joy
  • 1,765
  • 1
  • 14
  • 23
  • DUDE you are my angel now I realize my mistake I actually tried this code but I put the new `frmMain` first before `this.Hide();` – user2262382 Apr 15 '13 at 10:20
  • 1
    Really suggesting .Hide() in this situation? Seriously? The login should be closed altogether, not just hidden from the sight. – walther Apr 15 '13 at 10:27
  • @walther since login form is my startup form, if I will close my mainform the loginform will be closed too and I already tried it. – user2262382 Apr 15 '13 at 10:30
  • @user2262382, that doesn't matter, because there's no reason for loginform to still exist during the whole lifetime of your application. – walther Apr 15 '13 at 10:31
  • @walther yup all important is I can still no longer see that form when my main form starts thanks anyway for the advice. – user2262382 Apr 15 '13 at 10:36
  • @walther, i would be happy to see a better solution – r.hamd Aug 20 '15 at 13:17
0

loginform login = new loginfrom(); login.Hide();

Please try this way your login form hide.

mmpatel009
  • 921
  • 4
  • 11
  • 25
0

Your login form shouldn't be responsible for showing the main form, that is already handled later in your Main method! What you should do instead is to return the DialogResult, because later you're checking for its value.

I rather won't comment on your code, because it's an architectural suicide. Mixing various layers into one ugly mess, yuck.

The accepted answer suggesting .Hide() as a solution is BAD. Hide() is an equivalent of saying .Visible=false, that doesn't make sense in your scenario. It would still exist in the background.

walther
  • 13,466
  • 5
  • 41
  • 67
  • yes I am actually just a student I am new to everything this C# and mysql database everything is new to me :D – user2262382 Apr 15 '13 at 10:14
  • @user2262382, that's all nice, but if you were a building architect, you wouldn't put a toilet in the middle of the entrance hall, would you? It's kinda the same thing. You should learn good practices from the beginning or else you will have problems later. – walther Apr 15 '13 at 10:21
  • @user2262382, yea, couldn't resist. You've selected the wrong answer though... Hiding the form isn't really a good idea. Maciej just posted a better answer. – walther Apr 15 '13 at 10:30
  • I pinned my form on my taskbar and if it really still on the background the popup icon on the taskbar should still active but in my case it doen't popup anymore so, that means the form is totaly close. – user2262382 Apr 15 '13 at 10:34
0

Modify Main() as default:

 static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmLog ());
    }

Use this.Hide(); instead of this.Close();,try this:

private void btnSubmit_Click(object sender, EventArgs e)
    {
            Mylibrary a = new Mylibrary("localhost", "root", "", "cashieringdb");
            string user = txtLogin.Text;
            string pass = txtPassword.Text;
            string query = "SELECT * FROM register WHERE username='" + user + "' AND password=MD5('" + pass + "')";
            int result = a.Count(query);
            if (result == 1)
            {
                LOGIN_USER = txtLogin.Text;
                frmMain main = new frmMain();
                main.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Login Failed! Try Again");
                txtLogin.Text = "";
                txtPassword.Text = "";
            }

    }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • I feel that this might lead to memory leak. The **frmLog** keeps hidden in the memory even if the **frmMain** form is closed. So apparently the application would be running in the memory even if we quit it. – parkourkarthik Jul 19 '16 at 15:49
0

Your login form should be a separate one and you should show it as dialog from your main form. Your code could look like this:

private void btnSubmit_Click(object sender, EventArgs e)
{
    bool loginSuccessful = false;
    var frmLogin = new FormLogin();
    while (frmLogin.ShowDialog() == DialogResult.OK)
    {
        // verify login details
        Mylibrary a = new Mylibrary("localhost", "root", "", "cashieringdb");
        string user = frmLogin.txtLogin.Text;
        string pass = frmLogin.txtPassword.Text;
        string query = "SELECT * FROM register WHERE username='" + user + "' AND password=MD5('" + pass + "')";
        int result = a.Count(query);
        if (result != 1)
        {
            MessageBox.Show("Login failed, try again", "Login");
        }
        else
        {
            loginSuccessful = true;
            break;
        }
    }

    if (!loginSuccessful)
    {
        // user cancelled - close main window to end application
        this.Close();
    }
}
Maciej
  • 7,871
  • 1
  • 31
  • 36
0

A lot of Q/A suggested that You started correctly.
From Main() call LoginForm as modal and if result is OK LoginForm is automatically closed and MainForm is run by ApplicationRun(MainForm).

But, You allready called MainForm from LoginForm.

if (result == 1)
{
  LOGIN_USER = txtLogin.Text;
  frmMain main = new frmMain();
  main.Show();
  this.close
}

so change it with:

if (result == 1)
{
  LOGIN_USER = txtLogin.Text;
  //Logged in
  // return DialogResult.OK as it needed for Program.cs to allow load MainForm
  this.DialogResult = DialogResult.OK;
}

Modal Dialog Box - "...the code following it is not executed until after the dialog box is closed..."

Vladimir Vukanac
  • 944
  • 16
  • 29
0

You don't need hide first window when you show next window. Just in the Main method of Program class in the Program.cs file do this steps:

static void Main()
{
        if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 1)
        {
            return;
        }

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MyFirstWindow fw = null;
        do
        {
            fw = new MyFirstWindow();
            Application.Run(fw);
        } while (!fw.checked);

        Application.Run(new MyNextWindow());
    }

As you see in the MyFirstWindow class you have isChecked property which type is boolean and default value is false. So you can do some checks (login for example), change value of the checked property and close the first window. Your next window will not run untill the checked property will not be true