简体   繁体   English

表单打开后启用按钮C#

[英]Enabled the button after the form opened c#

currently my window is like this with the edit and delete button disabled . 目前,我的窗口是这样的, edit and delete button disabled了“ edit and delete button disabled In order to enable the buttons, user have to login with the administrator type . 为了enable the buttons, user have to login with the administrator type Right now, I already login with the administrator type from the member type. 现在,我已经使用成员类型中的管理员类型登录。 The disabled buttons supposed to be enabled after I logged in with the administrator type, but it is not. 我使用管理员类型登录后应该enableddisabled buttons ,但不是。

Is there any way to enable the button, after the form opened with the buttons disabled ? disabled按钮的情况下打开表单后,有什么方法可以enable按钮吗?

Here is the images: 这是图像:

As you can see on the below image, there is a admin login button with edit and delete buttons disabled. 如下图所示,有一个管理员登录按钮,其中禁用了编辑和删除按钮。 (Main System Form): (主系统表格):

在此处输入图片说明

Administrator Login (Privelege Form) 管理员登录 (权限表)

在此处输入图片说明

Here is the code that I am using: 这是我正在使用的代码:

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

Thank you. 谢谢。

I really appreciate your answer. 非常感谢您的回答。

There are several architectural issues here which when resolved will also make this function the way you want. 这里有几个体系结构问题,这些问题在解决后也会按您希望的方式发挥作用。 First of all it is not ideal to call a function from a form which will act upon that form. 首先,从将要作用于该形式的形式调用函数是不理想的。 It is a much better practice to return what is needed from that function and have the code to digest that result in the form which it affects. 更好的做法是从该函数返回所需的内容,并具有将结果转换为影响形式的代码。 Let's try a simple example of what the login button could do: 让我们尝试一个简单的示例,说明登录按钮可以做什么:

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

Obviously the only way this would work is if your login for was setting its DialogResult property, which is a simple way to pass a result from a modal dialog. 显然,唯一可行的方法是登录时设置其DialogResult属性,这是从模态对话框传递结果的简单方法。 We still have the issue of converting a login result to that value. 我们仍然存在将登录结果转换为该值的问题。 This can be addressed in the login button of the dialog, and the login method it calls. 这可以在对话框的登录按钮及其调用的登录方法中解决。

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

Now we have to change the AdminLogin method to a boolean: 现在,我们必须将AdminLogin方法更改为布尔值:

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

        if(isAdmin)
        {
            return true;
        }

        return false;
    }
}

This will make it easy to pass values as they are needed, without each object knowing more details about the other than is necessary. 这将使按需传递值变得容易,而每个对象不必知道其他有关对象的更多细节。 If the admin login needs to pass more information than just if the user is an admin, than create a class which contains all the different things one might want to know about the user's login and pass that as a return instead. 如果管理员登录名需要传递的信息不仅仅是用户是管理员,还需要创建一个类,该类包含所有可能要了解的用户登录名,并将其作为返回值传递。

您可以在此处执行的操作是,一旦用户在您的第一个表单上单击登录,就可以向第二个表单的构造函数发送布尔值,对admin表示true,对其他表单说false,并根据此值可以启用或禁用您的按钮。

The form load event, MainSystem_Load() , is only fired once (at first initialization). 表单加载事件MainSystem_Load()仅被触发一次(第一次初始化时)。 The UserPrivelege() function isn't called after the admin login. 管理员登录后未调用UserPrivelege()函数。 You will need to invoke that functionality after the admin logs in. 您需要在管理员登录后调用该功能。

assign value to UserInformation.CurrentLoggedInUserType and on click of Admin login button open your login form as dialog and after close of this form call UserPrivelege(); 将值分配给UserInformation.CurrentLoggedInUserType,然后单击“管理员”登录按钮,以对话框形式打开登录表单,并在关闭该表单后调用UserPrivelege();。 fuction 机能的研究

Admin login onclick :- 管理员登录onclick:-

  PrivelegeForm frm= new LoginForm();

   DialogResult result=  frm.ShowDialog();



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

don't forget to assign your static variable UserInformation.CurrentLoggedInUserType 不要忘记为您的静态变量分配UserInformation.CurrentLoggedInUserType

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM