表单打开后启用按钮c#

时间:2014-11-02 02:55:03

标签: c# winforms

目前我的窗口与edit and delete button disabled类似。为了enable the buttons, user have to login with the administrator type。现在,我已经使用成员类型的管理员类型登录。我使用管理员类型登录后disabled buttons应该是enabled,但事实并非如此。

在使用按钮enable打开表单后,有disabled按钮的任何方法吗?

以下是图片:

正如您在下图所示,有一个管理员登录按钮,其中禁用了编辑和删除按钮。(主系统表格):

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);
            }
        }

谢谢。

我非常感谢你的回答。

4 个答案:

答案 0 :(得分: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();
    }

现在我们必须将AdminLogin方法更改为布尔值:

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

        if(isAdmin)
        {
            return true;
        }

        return false;
    }
}

这将使得在需要时传递值变得容易,而不是每个对象都知道有关其他对象的更多细节。如果管理员登录需要传递的信息多于用户是管理员的信息,那么创建一个包含用户登录时可能想知道的所有不同内容的类,并将其作为返回传递。< / p>

答案 1 :(得分:0)

你可以在这里做的是,一旦用户点击你的第一个表单上的登录,你可以向你的第二个表单的构造函数发送一个布尔值,对于admin为true,对于其他表示为false,并且根据此值你可以启用或禁用您的按钮。

答案 2 :(得分:0)

表单加载事件MainSystem_Load()仅触发一次(首次初始化时)。管理员登录后不会调用UserPrivelege()功能。管理员登录后,您需要调用该功能。

答案 3 :(得分:0)

为UserInformation.CurrentLoggedInUserType分配值,点击Admin登录按钮打开登录表单作为对话框,关闭此表单后调用UserPrivelege();机能的研究

管理员登录onclick: -

  PrivelegeForm frm= new LoginForm();

   DialogResult result=  frm.ShowDialog();



    if (result==DialogResult.Ok)
    {
        UserPrivelege();
    }
不要忘记分配静态变量UserInformation.CurrentLoggedInUserType