2014-11-02 59 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