天天看點

c#兩種方式實作登入跳轉新窗體

兩種方式實作登入跳轉新窗體

  1. 直接隐藏登入窗體 顯示主界面
//驗證是否有此使用者
 int? flag = studentService.StudentLogin(userNameText.Text.Trim(), pwdText.Text.Trim());
if (flag != null && flag != 0) {
//建立執行個體,傳值
Home home = new Home(userNameText.Text.Trim());
//隐藏登入視窗
this.Hide();
//顯示主視窗
new Home().Show();
}
           
public Home(string userName) {
        InitializeComponent();
        this.userName = userName;
     }
private void Home_Load(object sender, System.EventArgs e) {
    this.textBox1.Text = this.userName;
   }
           
//直接運作登入視窗
Application.Run(new Login());
           
  1. 在program.cs中判斷當登入頁面傳回登入成功時 運作主窗體
public static string userName;
           
//驗證是否有此使用者
if (flag != null && flag != 0) {
this.DialogResult = DialogResult.OK;
Home.userName = userNameText.Text.Trim();
this.Close();
           
Application.EnableVisualStyles();   Application.SetCompatibleTextRenderingDefault(false);
Login login = new Login();
login.ShowDialog();
if (login.DialogResult == DialogResult.OK) {
Application.Run(new Home());
}
           
private void Home_Load(object sender, System.EventArgs e) { this.textBox1.Text = userName;
  }