如何从点击事件中获取自动生成的按钮的具体信息

How to get specific information about an automatically generated button from a click event

在此方法中,按钮会根据 sql 查询显示的结果数量自动生成到流布局面板。然后将新按钮添加到事件处理程序以实现功能。我的问题是,我想获取特定于我单击的按钮的信息。分配给按钮的信息是 clientName、projectType,并且在后台包含属性 clientNo 和 projectNo 以实现参照完整性。我需要的是单击按钮时分配给该按钮的值。

目前,该方法仅返回最后创建的按钮。

private string AddButton()
        {

            string sql = "SELECT projectType, clientName, projectNo, Client.clientNo FROM Project, Client WHERE Project.ClientNo = Client.ClientNo;";

            newConnection = new ConnectToDatabase(sql);

            dataReader = newConnection.NewDataReader();

            while (dataReader.Read())
            {
                newButton = new Button();
                newButton.Font = new Font("Georgia", 12);
                newButton.Size = new Size(194, 80);
                newButton.Name = dataReader.GetValue(3).ToString();
                newButton.Text = dataReader.GetValue(1).ToString() + "\n" + dataReader.GetValue(0);
                flpDisplayProjects.Controls.Add(newButton);
                newButton.Click += new System.EventHandler(newButton_Click);
            }

            return newButton.Name;
        }

        void newButton_Click(object sender, EventArgs e)
        {
            string sql = "SELECT projectType, clientName, projectNo FROM Project, Client WHERE Project.ClientNo = Client.ClientNo AND " +
                "clientName = '" + newButton.Name + "';";

            newConnection = new ConnectToDatabase(sql);

            dataReader = newConnection.NewDataReader();

            timeTrackingForm newForm = new timeTrackingForm(dataReader);
            newForm.Show();
        }

触发事件的按钮在 sender 参数中可用...只需将其投回:

void newButton_Click(object sender, EventArgs e)
{
    Button myButton = (Button)sender;

    // do whatever you want with myButton, not newButton

    ...

不要使用 newButton,它只是对您创建的最后一个按钮的引用。实际上,无论创建这些按钮的方法是什么,都应该在 内部 定义该变量。

您可以使用 lambda 表达式来捕获 lambda 之外的任何值。

您的代码如下所示:

private string AddButton()
{
    string sql = "SELECT projectType, clientName, projectNo, Client.clientNo FROM Project, Client WHERE Project.ClientNo = Client.ClientNo;";
    newConnection = new ConnectToDatabase(sql);
    dataReader = newConnection.NewDataReader();
    while (dataReader.Read())
    {
        newButton = new Button();
        newButton.Font = new Font("Georgia", 12);
        newButton.Size = new Size(194, 80);
        newButton.Name = dataReader.GetValue(3).ToString();
        newButton.Text = dataReader.GetValue(1).ToString() + "\n" + dataReader.GetValue(0);
        flpDisplayProjects.Controls.Add(newButton);
        newButton.Click += (s, e) =>
        {
            string sql = "SELECT projectType, clientName, projectNo FROM Project, Client WHERE Project.ClientNo = Client.ClientNo AND " +
            "clientName = '" + newButton.Name + "';";
            newConnection = new ConnectToDatabase(sql);
            dataReader = newConnection.NewDataReader();
            timeTrackingForm newForm = new timeTrackingForm(dataReader);
            newForm.Show();
        };
    }
    return newButton.Name;
}

Click 处理程序内部对 newButton 的引用与外部相同。

这也消除了创建 newButton_Click 方法的需要 - 这通常有利于封装功能。