2013-08-29 73 views
1

我想向我的數據庫應用程序添加一個安全模塊,實際上我試圖做的是每當我點擊登錄按鈕時,它退出登錄窗體並打開取決於用戶角色的主要形式。 我有以下代碼:點擊登錄按鈕從一個窗體重定向到其他窗體

登錄表單:

public void Login() 
    { 
     frmCommissionReport _commReport = new frmCommissionReport(); 

     if (_commission.Login(cbxLoginName, txbPassword)) 
     { 
      MessageBox.Show("Successfull"); 
      // close this form - do not exit the application 
      frmCommissionReport frm = new frmCommissionReport(); 
      this.Close(); 
      frm.ShowDialog(); 

     } 
     else 
     { 

      MessageBox.Show("Username or Password not recognised"); 

     } 

    } 

    private void btnLogin_Click(object sender, EventArgs e) 
    { 
     Login(); 

    } 

主要形式(frmCommissionReport)

public frmCommissionReport() 
    { 

     InitializeComponent(); 

     _login.ShowDialog(); 

    } 

但因爲某些原因,每當我填在登錄中詳細信息,然後點擊登錄按鈕,再次打開相同的登錄表單,並當您成功登入重複每當我點擊登錄按鈕登錄表單,而不是重定向我主窗體

enter image description here

+0

什麼'_login'在主窗體構造函數工作得很好? – DGibbs

+0

@DGibbs這是一個登錄對象 – DoIt

+0

因此,當主窗體加載時,它顯示登錄窗體...? '但是由於某些原因,每當我填寫登錄詳細信息並單擊登錄按鈕時,它會再次打開相同的登錄表單,您對此感到驚訝嗎?嘗試刪除該行。 – DGibbs

回答

1

我想我的問題的解決方案易於在一個有點不同的方式比我開始與

登錄表單

public partial class frmLogin : Form 
{ 

    #region "Properties" 

    private bool _Authenticated = false; 

    public bool Authenticated 
    { 
     get { return _Authenticated; } 
     set { _Authenticated = value; } 
    } 

    #endregion 
public void Login() 
    { 

     if (GetLoginAuthentication(cbxLoginName, txbPassword)) 
     { 
      this.Hide(); 
      //MessageBox.Show("Successfull"); 
      Authenticated = true; 
      //frmCommissionReport frm = new frmCommissionReport(); 
      //frm.ShowDialog(); 


     } 
     else 
     { 
      Authenticated = false; 
      MessageBox.Show("Username or Password not recognised"); 

     } 

    } 

主要形式

public frmCommissionReport() 
    { 
     frmLogin login = new frmLogin(); 
     login.ShowDialog(); 

     if (login.Authenticated) 
     {// block of code 
} 

這沒有任何問題

2

不斷,你是實例化另一個frmCommissionReport(),後者又調用_login.ShowDialog()(我假設在一個新的Login實例上),並且this.Close()將關閉現有的Login表單,而不是與新的關聯的新表單frmCommissionReport()實例。

+0

這是我查看代碼時看到的。我相信你是正確的,'_login.ShowDialog();'正在調用登錄表單來顯示另一個表單。 – Magnum

+0

@Thabo謝謝,我刪除了它,但是當我點擊登錄時,它仍然沒有將我重定向到主窗體 – DoIt

+0

主窗體已經實例化,所以用它來控制應用程序中的其他對話框,而不是Login類。將所有frmCommisionReports從登錄中取出,並將其邏輯放在主窗體中。獲取_login.ShowDialog的返回值並在InitializeComponents()之前調用它,以便在創建主窗體上的組件之前循環登錄窗體或彈出窗口或其他內容。 – Thabo

0

試圖改變this.close到this.hide

if (_commission.Login(cbxLoginName, txbPassword)) 
    { 
     MessageBox.Show("Successfull"); 
     // close this form - do not exit the application 
     frmCommissionReport frm = new frmCommissionReport(); 
     this.hide(); 
     frm.ShowDialog(); 

    } 
相關問題