I have a form1 as a parent form (and is a MDI container) which is a parent for a lot of the other child forms. Now I would like to create a Login. It should be another windows form - formLogin called. And it has to start the 1st when application is started (so it has to start before form1). Is this possible?

--------------------------------------------------------------------------------------
What I did for a test:
I tryed to set FormLogin as a parent (and MDI contrainer = true), and form1 (which it wasn`t any more a parent, and not even a MDI) and I set in the program.cs file: Application.Run(new FormLogin());
The formLogin is started, then it goes through the login code, if the username and pw are correct I simply did:

Fom1 = new Form1();
form1.MdiParent = this;
form1.Show();

But it does not open. I don`t know why.
----------------------------------------------------
But I believe this is not the correct way, it can not be the loginForm a parent, it should be the Form1.

Edited 7 Years Ago by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Your Application.Run should run your parent form. Application.Run(new Form1()); in your Form1 FormLoad event you create and show the login form. By showing it in the form load it will appear before the parent:

private void Form1_Load(object sender, EventArgs e)
        {
            FormLogin login = new FormLogin();
            login.MdiParent = this;
            login.ShowDialog();
        }

By calling ShowDialog() you block Form1 until the login is finished so Form1 wont appear until you close/return the login form.
You will probably want to include a public property in the Login Form to allow Form1 to retreive the login credentials.
You can also put code in the form load to process the login before the MDI Parent is shown; show certain controls based on user credentials etc.

You can also put code in the form load to process the login before the MDI Parent is shown; show certain controls based on user credentials etc.

How to do that?

This is what I did so far in LoginForm:

private void buttonLogin_Click(object sender, EventArgs e)
         {
            string username = textBoxIme.Text;
            string password = textBoxGeslo.Text.Trim();
            CheckingLogin(username, password);
            //What to write here when code returns from CheckLogin methnd?
            //If returns with true it has to close() LoginForm and show Form1
            //Else if returns with false, it has to show loginForm ones again (and not allow to go into Form1)
            
        }

        static string sql_Conn = ConfigurationManager.ConnectionStrings["sqlConn"].ConnectionString;
        public static bool ChecingLogin(string userName, string password)
        {
            bool myResult = false;
            
            //The rest of the login form, which checks username with pw            
       }

Edited 7 Years Ago by Mitja Bonca: n/a

Firstly, i just realised a mistake in my original code. You need to remove login.MdiParent = this; as you cant open a child form modally.

Rather than closing the form when they have logged in you will want to hide it so you can still retrieve the username/password entered:

string username = textBoxIme.Text;
string password = textBoxGeslo.Text.Trim();
if(CheckingLogin(username, password)==true)
{
    this.Hide();
}
else
{
MessageBox.Show("Invalid username/password");
}

There is a very good guide by sknake available that you should look at: Sample C# Windows Form Login Screen

Thank you, just one more thing:
Did it:

string username = textBoxIme.Text;
string password = textBoxGeslo.Text.Trim();
if (CheckingLogin(username, password) == true)
            {
                //if its ok
            }

btw, what to put in the brackets if I did in Program.cs:

using (LoginForm login = new LoginForm()) 
{
    if (login.ShowDialog() == DialogResult.OK)
          Application.Run(new Form1());
}

If you wanted to do it that way you would need to set the LoginForms dialogresult property like this:

if (CheckingLogin(username, password) == true)
            {
                this.DialogResult = DialogResult.OK;
            }

Just be aware that if they enter the wrong details and close the login form then the application will close.
You might want to add in a finite loop which allows 3 attempts before the application closes.

This question has already been answered. Start a new discussion instead.