3

I am working on a project (simple phonebook) for personal use.

Basically, I have got three forms: Main (the main one), Settings (the one used for configuring settings) and Login (the login form, which should start only if the user checked the option to be asked for the password on startup). Just to make the things clear: when the .EXE file is started it should load the Main form, unless the option to ask for pass is checked (Properties.Settings.Default.AskForPass == true) - when it should run the Login form first.

Here is what the login form looks like:

Login

I have no idea how to make things work in the proper way.

I have tried to change the line in Program.cs from Main to Login:

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

and then if the user enters the right username and password and clicks the OK button:

private void button1_Click(object sender, EventArgs e)
    {
        if (txtUsername.Text == Properties.Settings.Default.MyUsername && txtPassword.Text == Properties.Settings.Default.MyPassword)
        {
            Main f1 = new Main();
            Login f3 = new Login();
            f1.Show();
            f3.Close();
        }
        else
        {
            DialogResult dialogResult = MessageBox.Show("Wrong password! Do you want to try again?", "Warning", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning);
            if (dialogResult == DialogResult.Retry)
            {
                return;
            }
            else if (dialogResult == DialogResult.Cancel)
            {
                this.Close();
            }
        }
    }

the login form stays in the background, as you can see here:

Contacts

I would like the Login form to be closed, so it doesn't appear in the background. The problem is that now the Login form is the main one and if I try to close it, the whole application shuts down.

I have tried changing the owner but had no success doing so.

Any ideas? Please, help!

Exinta Enea
  • 235
  • 3
  • 5
  • 16
  • Check here if it helps you . http://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi – Suraj Singh Nov 26 '13 at 15:55
  • `f1` and `f3` is confusing. If you have more than one variable in a block, it is helpful to use more descriptive names like `formMain` and `formLogin` – rhughes Nov 26 '13 at 15:58

1 Answers1

5

You are creating a new Login form and closing that. Try

this.Hide();
f1.ShowDialog();
this.Close();

Another approach is to return a status on the Login Form and then check that and Application.Run() the Main form:

In the login form:

private bool LoggedIn;
public bool IsLoggedIn { get { return LoggedIn; } }

Then in the button click:

LoggedIn = true; 
this.Close();

And in Program.cs:

Login f = new Login();
Application.Run(f);

if (f.IsLoggedIn)
    Application.Run(new Main());
John Koerner
  • 37,428
  • 8
  • 84
  • 134
  • Thank you very much, this works fine. But, I want form **Main** to be the main one, and now that is not the case. Now, the main one is **Login**. How can I make the **Login** form appears first (only if Properties.Settings.Default.AskForPass == true), while form **Main** is actually the main one? – Exinta Enea Nov 26 '13 at 15:48
  • Thank you very much, this works perfectly. I have added `if (Properties.Settings.Default.AskForPass == true)` as well. – Exinta Enea Nov 26 '13 at 16:14