0

I'm a newbie so please be patient with me. I've two forms, one is the main form and the other is the login form. On start up, I would like to have the login form show up so the user can enter their credentials and upon successfully login I want the main form to show. What is the best way of doing this? Please post some tips. Thanks in advance.

3
Contributors
3
Replies
4
Views
4 Years
Discussion Span
Last Post by Mitja Bonca
0

open your Program.cs file in your solution, and in the Application.Run(); type new MyLoginForm so would be like:

            Application.Run(new LoginForm());

This specifies the form that you wish to run first.

Hope this helps.

0

open your Program.cs file in your solution, and in the Application.Run(); type the word new followed by the name of the form you wantto be run first. so would be like:

            Application.Run(new LoginForm());

This specifies the form that you wish to run first.

Hope this helps.

Edit from above (not used to new site).

Edited by ChrisHunter: unfamiliar with new site

0

I would do it differenty. I would run Login form before starting the main form (with application.Run).
Take a look at here:

 static class Program
 {
   [STAThread]
   static void Main()
   {   
       using (Login login = new Login())
       {
            login.StartPosition = FormStartPosition.CenterScreen;
            if (login.ShowDialog() == DialogResult.OK)
            {      
                  Application.Run(new Form1(login.strUserName)); //passing the userName to the constructor of form1 (from Login form - global variable)!
            }
       }
    }
}

//form1:
public partial class Form1 : Form
{  
     string userName; 
     public Form1(string _strUser)
     {   
           InitializeComponent();
           userName = _strUser; //a local variable of a form1 class has hold user`s name (userName - which u can call it from within the form1 class!
     }
}

public partial class Login : Form
{
       public string UserName { get; private set; } 
       public Login()
       {
              InitializeComponent();
       }

       private void btn_Login_Click(object sender, EventArgs e)
       {
             string userName = textBox1.Text.Trim();
             string pswd = textBox2.Text.Trim();
             bool bLoginOk = CheckUser(userName, pswd);
             if (bLoginOK)
             {
                  this.DialogResult = DialogResult.OK;
                  this.UserName = userName; //setting global variable (a property) so it can get the name out of it later (from program.cs)
                  //code will close login form, and go back to program.cs ...
             }
             else
            {
                 //you can show here a messageBox that userName and password do not match if you like!
                 //AND the login form still stays open!!
            }
      }


      private bool CheckUser(string name, string pswd)
      {
            bool bChecking = false;

            //do the login checking, if username and password match, return true, else false

            //if username and password match:
            bChecking = true;

            return bChecking;
      }     
}

Edited by Mitja Bonca: re-edit the code! Still learning new style :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.