2017-03-04 25 views
0

我的项目中有两种形式(LoginMain)。我试图找到的是,如果登录成功,我必须显示Main表单并关闭Login表单。当登录成功时,我在Login表单中使用此方法关闭Login表单。但是当我关闭登录表单时,所有表单都关闭。登录表格关闭问题

+1

显示你的代码,请。 –

+0

看起来像你的登录表单以第一种形式启动(在'Program.cs'中,'Application.Run(new Form1());')。改变这一点,以便Application.Run启动主表单,隐藏它,显示登录表单,如果登录有效,关闭登录表单并显示主表单。 – Nino

+0

可能从http://stackoverflow.com/questions/4759334/how-can-i-close-a-login-form-and-show-the-main-form-without-my-application-closi复制 – Xavave

回答

0

你可以做到这一点,你的Program.cs中,在后台运行的主要形式,并显示它,当你成功授权用户:

的Program.cs:

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

然后在你的主要形式,在成功授权,表现出来,在这种情况下,我把它放在构造函数:

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

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

确保您的登录表单上,在成功authorizati添加这行代码上:

this.DialogResult = DialogResult.OK; 
0

你可以把登录表单,你检查登录成功的代码如下。

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

     } 
     this.Close(); 
0

u能在Program.cs中时启动的应用程序的main方法直接使用登录表单..

该样品conains 2种形式命名的MainForm和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

这个类包含loged用户信息

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

这种形式打开自己并检查用户并返回结果

你应该设置取消的DialogResult财产取消按钮

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