2014-11-02 66 views
0

當前我的窗口是這樣的edit and delete button disabled。爲了enable the buttons, user have to login with the administrator type。現在,我已經使用成員類型的管理員類型登錄。在我用管理員類型登錄後,disabled buttons應該是enabled,但事實並非如此。啓用窗體打開後的按鈕c#

有沒有什麼辦法給enable這個按鈕,用按鈕打開表格後disabled

以下是圖像:

正如你可以下面的圖片上看到,有一個與編輯admin登錄按鈕和刪除按鈕禁用。(主系統表):

enter image description here

管理員登錄(Privelege表)

enter image description here

這裏是我使用的代碼:

public class SystemManager 
{ 
    public static void AdminLogin(string _value1, string _value2, Form _windowsForm, TextBox _windowsTextBox) 
      { 
       using (OleDbConnection connection = new OleDbConnection(connectionString)) 
       { 
        string query = "SELECT * FROM [Member] WHERE [Username] = @Username"; 

        connection.Open(); 

        using (OleDbCommand command = new OleDbCommand(query, connection)) 
        { 
         command.Parameters.Add("@Username", OleDbType.VarChar); 
         command.Parameters["@Username"].Value = _value1; 

         using (OleDbDataReader reader = command.ExecuteReader()) 
         { 
          if (reader.Read()) 
          { 
           string password = (string)reader["Password"]; 
           string userType = (string)reader["UserType"]; 

           _isValidPassword = BCrypt.ValidateHash(_value2, password); 

           if (userType == "Administrator") 
           { 
            _isAdministrator = true; 
           } 

           else if (userType != "Administrator") 
           { 
            _isAdministrator = false; 
           } 

           if (_isValidPassword && _isAdministrator) 
           { 
            Authenticate _authenticate = new Authenticate(); 

            _authenticate.ShowDialog(); 

            ShowMessageBox("Authenticated.", "Success", 2); 

            UserInformation.isAdministrator = true; 

            _windowsForm.Hide(); 

            _windowsForm.Close(); 
           } 

          } 

          if (!_isValidPassword || !_isAdministrator) 
          { 
           Authenticate _authenticate = new Authenticate(); 

           _authenticate.ShowDialog(); 

           ShowMessageBox("Either username or password incorrect or you are not administrator. Please try again.", "Error", 1); 

           ClearTextBoxes(_windowsForm.Controls); 

           _windowsTextBox.Focus(); 
          } 

          reader.Close(); 
         } 
        } 

        connection.Close(); 
       } 
      } 
} 

public partial class MainSystem: Form 
{ 
void MainSystem_Load(object sender, EventArgs e) 
     { 
      UserPrivelege(); 
     } 

    void UserPrivelege() 
      { 
       if (UserInformation.CurrentLoggedInUserType == "Member") 
       { 
        this.button3.Enabled = false; // Edit Button 
        this.button4.Enabled = false; // Delete Button 
        this.button7.Enabled = false; 
        this.button9.Enabled = true; // Admin Login Button 
       } 

       else if (UserInformation.CurrentLoggedInUserType == "Administrator" || UserInformation.isAdministrator) 
       { 
        this.button3.Enabled = true; // Edit Button 
        this.button4.Enabled = true; // Delete Button 
        this.button7.Enabled = true; 
        this.button9.Enabled = false; // Admin Login Button 
       } 

      } 
} 

public partial class Privelege : Form 
    { 
     void button1_Click(object sender, EventArgs e) // OK Button 
     { 
      Check(); 
     } 

     void Check() 
     { 
      if (this.textBox1.Text == string.Empty || string.IsNullOrWhiteSpace(this.textBox1.Text)) 
      { 
       SystemManager.ShowMessageBox("Username field required.", "Information", 2); 
      } 

      else if (this.textBox2.Text == string.Empty || string.IsNullOrWhiteSpace(this.textBox2.Text)) 
      { 
       SystemManager.ShowMessageBox("Password field required.", "Information", 2); 
      } 

      else 
      { 
       SystemManager.AdminLogin(this.textBox1.Text, this.textBox2.Text, this, this.textBox1); 
      } 
     } 

謝謝您。

我真的很感謝你的回答。

回答

1

有幾個建築在這裏的問題從而解決也將使這個功能你想要的方式時。首先,從表單中調用函數並不理想。返回該函數所需的內容並使代碼消化以產生影響的形式是一種更好的做法。讓我們嘗試什麼樣的登錄按鈕可以做一個簡單的例子:

private void btnLogin_Click(object sender, EventArgs e) 
    { 
     var login = new LoginForm(); 

     login.ShowDialog(); 

     var result = login.DialogResult == System.Windows.Forms.DialogResult.Yes; 

     if (result) 
     { 
      button2.Enabled = true; 
      button3.Enabled = true; 
     } 
    } 

顯然只有這樣的工作方式是,如果您的登錄被設置它的DialogResult屬性,這是一種簡單的方法來從一個模態傳遞一個結果對話。我們仍然存在將登錄結果轉換爲該值的問題。這可以在對話框的登錄按鈕和它所調用的登錄方法中解決。

private void btnDialogLogin_Click(object sender, EventArgs e) 
    { 
     // Form validation here... 

     var result = SystemManager.AdminLogin(NameButton.Text, PassButton.Text); 

     DialogResult = DialogResult.No; 

     if (result) 
     { 
      DialogResult = DialogResult.Yes; 
     } 

     this.Close(); 
    } 

現在我們要改變後臺管理方法爲布爾:

public class SystemManager 
{ 
    public static bool AdminLogin(string _value1, string _value2) 
    { 
     // Database and evluation... 

     if(isAdmin) 
     { 
      return true; 
     } 

     return false; 
    } 
} 

這將使得在需要的時候可以方便地傳遞值,沒有每個對象知道關於其他更多的細節比必要。如果管理員登錄需要傳遞比用戶是管理員更多的信息,則需要創建一個類,其中包含所有人可能想知道的用戶登錄信息,然後將其作爲回報傳遞給用戶。

+0

尼斯閱讀和結構+1。先生,非常感謝Lathejockey81 – 2014-11-02 11:20:55

0

你可以做什麼這裏,一旦用戶點擊你的第一個表格上登錄,你可以一個布爾值發送到你的第二個窗體的構造函數,表示對admin真假他人,取決於這個值就可以啓用或禁用您的按鈕。

0

的形式加載事件,MainSystem_Load(),僅觸發一次(在第一次初始化)。管理員登錄後不會調用UserPrivelege()函數。您將需要管理員登錄後調用該功能性。

0

將值賦給UserInformation。CurrentLoggedInUserType和點擊管理員登錄按鈕打開你的登錄表單作爲對話框,並在此表單關閉後調用UserPrivelege();機能的研究

管理員登錄的onclick: -

PrivelegeForm frm= new LoginForm(); 

    DialogResult result= frm.ShowDialog(); 



    if (result==DialogResult.Ok) 
    { 
     UserPrivelege(); 
    } 

不要忘了你的分配靜態變量UserInformation.CurrentLoggedInUserType