简体   繁体   English

一个按钮可以有多种方法?

[英]single button can have multiple methods?

I have these 4 buttons. 我有这四个按钮。

truncateEmp truncateStocks truncateLogs truncateExpenses truncateEmp truncateStocks truncateLogs truncateExpenses

If I press one of these, a new form will open, and will ask the user for the password from the database. 如果我按其中之一,将打开一个新表格,并要求用户从数据库中输入密码。 If the password is correct, it will truncate the table. 如果密码正确,它将截断该表。

The new form has two buttons, proceed and cancel . 新表单有两个按钮,“ proceed和“ cancel Of course, cancel , closes the form and proceed will check the user's pass if its correct, then do it's job. 当然, cancel ,关闭表格,然后proceed检查用户的通行证是否正确,然后进行操作。

Is it possible if proceed can truncate other tables? 如果proceed可以截断其他表,是否可以? I can only truncate Employees table because proceed_Click has only one method. 我只能截断Employees表,因为proceed_Click只有一种方法。

private void proceed_Click(object sender, EventArgs e)   //proceed
    {

        string pass = passField.Text;
        if (pass == "")
        {
            MessageBox.Show("Did you input something? I doubt it.");
            return;
        }

        bool r = validate_login(pass);
        if (r)
        {
            db_connection();

            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = "TRUNCATE TABLE Employees";
            cmd.Connection = connect;

            cmd.ExecuteNonQuery();
            MessageBox.Show("Success!", "TRUNCATE", MessageBoxButtons.OK, MessageBoxIcon.Information);

            emp.ShowDataToGrid();
            logs.Log_TruncateEmp();
            this.Close();
            passField.Clear();

            connect.Close();
        }
        else
        {
            MessageBox.Show("Wrong password!", "TRUNCATE", MessageBoxButtons.OK, MessageBoxIcon.Error);
            passField.Clear();
        }
    }

    private void cancel_Click(object sender, EventArgs e) //cancel
    {
        this.Close();
    }   
}

Thank you! 谢谢!

Button.Click is an event , and yes, multiple methods may be attached to such events. Button.Click是一个事件 ,是的,可以将多个方法附加到此类事件。

The way methods work is basically that multiple actions may be executed if an event triggers. 方法的工作方式基本上是,如果事件触发,则可以执行多个动作。 So if you have 3 methods assigned to your proceed.Click event, then all 3 will execute. 因此,如果为您的proceed.Click事件分配了3个方法,则所有3个方法都将执行。

Here is an example, showing how 2 click events can be attached to a single button: 这是一个示例,显示如何将2个单击事件附加到单个按钮:

public void Form_Load(object sender, EventArgs e)
{
    proceed.Click += MyClickEvent1;
    proceed.Click += MyClickEvent2;
}

public void MyClickEvent1(object sender, EventArgs e)
{
    // Do Stuff ...
}
public void MyClickEvent2(object sender, EventArgs e)
{
    // Do More Stuff ...
}

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

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