簡體   English   中英

表單打開后啟用按鈕C#

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

目前,我的窗口是這樣的, edit and delete button disabled了“ edit and delete button disabled 為了enable the buttons, user have to login with the administrator type 現在,我已經使用成員類型中的管理員類型登錄。 我使用管理員類型登錄后應該enableddisabled buttons ,但不是。

disabled按鈕的情況下打開表單后,有什么方法可以enable按鈕嗎?

這是圖像:

如下圖所示,有一個管理員登錄按鈕,其中禁用了編輯和刪除按鈕。 (主系統表格):

在此處輸入圖片說明

管理員登錄 (權限表)

在此處輸入圖片說明

這是我正在使用的代碼:

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

謝謝。

非常感謝您的回答。

這里有幾個體系結構問題,這些問題在解決后也會按您希望的方式發揮作用。 首先,從將要作用於該形式的形式調用函數是不理想的。 更好的做法是從該函數返回所需的內容,並具有將結果轉換為影響形式的代碼。 讓我們嘗試一個簡單的示例,說明登錄按鈕可以做什么:

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

這將使按需傳遞值變得容易,而每個對象不必知道其他有關對象的更多細節。 如果管理員登錄名需要傳遞的信息不僅僅是用戶是管理員,還需要創建一個類,該類包含所有可能要了解的用戶登錄名,並將其作為返回值傳遞。

您可以在此處執行的操作是,一旦用戶在您的第一個表單上單擊登錄,就可以向第二個表單的構造函數發送布爾值,對admin表示true,對其他表單說false,並根據此值可以啟用或禁用您的按鈕。

表單加載事件MainSystem_Load()僅被觸發一次(第一次初始化時)。 管理員登錄后未調用UserPrivelege()函數。 您需要在管理員登錄后調用該功能。

將值分配給UserInformation.CurrentLoggedInUserType,然后單擊“管理員”登錄按鈕,以對話框形式打開登錄表單,並在關閉該表單后調用UserPrivelege();。 機能的研究

管理員登錄onclick:-

  PrivelegeForm frm= new LoginForm();

   DialogResult result=  frm.ShowDialog();



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

不要忘記為您的靜態變量分配UserInformation.CurrentLoggedInUserType

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM