0

I have two forms in my project (Login and Main). What I'm trying to accoomplish is, if the login is successful, I must show the Main form and close the Login form. I have this method in Login form that closes the Login form when the login is successful. But when I close login form, all forms closed.

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
  • 1
    Show your code please. – Salah Akbari Mar 04 '17 at 08:33
  • looks like your login form is started as first form (in `Program.cs`, line `Application.Run(new Form1());`). Change that so that `Application.Run` starts main form, hides it, shows login form and if login is valid, closes login form and shows main form. – Nino Mar 04 '17 at 08:34
  • possible duplicate from http://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi – Xavave Mar 04 '17 at 08:34

3 Answers3

0

You can do this, on your Program.cs, run your main form on the background and show it when you successfully authorized the user:

Program.cs:

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainForm frm = new MainForm();
        Application.Run();
    }

Then on your Main Form, upon successful authorization, show it, in this case I put it on the constructor:

 public MainForm()
    {
        Login frmLogin = new Login();
        frmLogin.Show();

        if (frmLogin.ShowDialog(this) == DialogResult.OK)
        {
            this.Show();
            InitializeComponent();
        } 
    }

Make sure on your Login Form, add this line of code upon successful authorization:

this.DialogResult = DialogResult.OK;
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57
0

You can put on the login form where you check the login is successful the code below.

        Form2 formmain = new Form2();
        this.Hide();
        if (formmain.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Form2());

        }
        this.Close();
tzavsk
  • 11
  • 6
0

u can use login form directly in main method on program.cs when app start..

this sample conains two form named MainForm and FrmLogin..

program.cs

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        FrmLogin frmLogin = new FrmLogin();
        UserInfo userInfo = frmLogin.Login();
        if (userInfo != null)
        {
            // open main form with current user
            Application.Run(new MainForm(userInfo));
        }
    }

UserInfo.cs

this class contains loged user info

public class UserInfo
{
    // this fields are samples. 
    // you can add  what do you need..
    public int Id { get; set; }
    public string UserName { get; set; }
    public DateTime LoginDate { get; set; }
    public string Roles { get; set; }
}

FrmLogin

This form opens itself and checks the User and returns the result

you should set Cancel to Dialogresult property of Cancel button

// you need add these controls to form
// txtUserName(TextBox)
// txtPassword(TextBox)
// btnOK (Button) // need click event
// btnCANCEL(Button)
public partial class FrmLogin : Form
{
    public FrmLogin()
    {
        InitializeComponent();
    }
    UserInfo currentUser;
    private void btnOK_Click(object sender, EventArgs e)
    {
        var userName = txtUserName.Text;
        var password = txtPassword.Text;
        currentUser = GetUser(userName, password);
        if (currentUser == null)
        {
            MessageBox.Show("invalid username | password");
            this.DialogResult = DialogResult.Cancel;
        }
        this.DialogResult = DialogResult.OK;
    }

    public UserInfo Login()
    {
        var dialogResult = this.ShowDialog();
        if (dialogResult != DialogResult.OK)
            return null;
        return currentUser;
    }
    private UserInfo GetUser(string userName,string passwrod)
    {
        // you should check from where users located area(like db)
        if (userName.Equals("admin") && passwrod.Equals("test"))
        {
            return new UserInfo {
                Id = 1,
                LoginDate = DateTime.Now,
                Roles = "Admin",
                UserName ="admin"
            };
        }
        return null;
    }

}

MainForm

UserInfo _currentUser;

public partial class MainForm : Form
{
    UserInfo _currentUser;

    public MainForm(UserInfo user)
    {
        _currentUser = user;
        InitializeComponent();
    }
}
levent
  • 3,464
  • 1
  • 12
  • 22