標籤:his pass end message gre 不可 winform run div
在winform 介面編程中,我們有時候要在主介面開啟之前先顯示登入介面,當登入介面使用者資訊校正正確後才開啟主介面,而這時登陸介面也完成使命該功成身退了。
目前有兩種方法可實現:
方法1. 隱藏登入介面
Program.cs 中代碼如下:
/// <summary> /// 應用程式的主進入點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Login()); }
Login.cs 中代碼如下:
/// <summary> /// login /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) { if (txtPassword.Text == "12345678") { UI ui = new UI(); this.Visible = false; ui.ShowDialog();//此處不可用Show() this.Dispose(); this.Close(); } else { MessageBox.Show("Password is incorrect ", "Prompt message",MessageBoxButtons.OK,MessageBoxIcon.Error); } } /// <summary> /// exit /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExit_Click(object sender, EventArgs e) { this.Dispose(); this.Close(); }
方法2. 登入介面以 dialog形式開啟,返回登入結果
Program.cs 中代碼如下:
/// <summary> /// 應用程式的主進入點。 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Login login = new Login(); login.ShowDialog(); if (login.DialogResult == DialogResult.OK) { login.Dispose(); Application.Run(new UI()); } else if (login.DialogResult == DialogResult.Cancel) { login.Dispose(); return; } }
Login.cs 中代碼如下:
/// <summary> /// login /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnLogin_Click(object sender, EventArgs e) { if (txtPassword.Text == "12345678") { this.DialogResult = DialogResult.OK; this.Dispose(); this.Close(); } else { MessageBox.Show("Password is incorrect ", "Prompt message",MessageBoxButtons.OK,MessageBoxIcon.Error); } } /// <summary> /// exit /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExit_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Dispose(); this.Close(); }
C# winform 主介面開啟並關閉登入介面