简体   繁体   中英

Login form Closing Problems

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.

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;

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();

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM