【问题标题】:How to know which button was clicked in winforms c#?如何知道在winforms c#中单击了哪个按钮?
【发布时间】:2016-04-24 23:14:12
【问题描述】:

好的,所以这有点难以解释。

我正在制作一个基本的时间表表格。

我有 7 个按钮,分别命名为 btnMonTimebtnTueTime 等等,直到 btnSunTime 基于星期几。现在,每次单击按钮时,都会打开一个弹出窗口(winform),让用户通过dateTimePicker 控件选择某个时间。时间被解析为字符串并存储。弹出窗口上有一个接受按钮,当按下该按钮时,弹出窗口将关闭,并在特定日期旁边显示一个标签,说明时间已发布。

`

现在我知道如何在某一天执行此操作,但问题是我只有一个函数来创建此标签。但是我怎么知道点击了哪个时间按钮将其放置在正确的位置?

这是我能想到的代码:

private void btnAccept_Click(object sender, EventArgs e)
{
        formPopup.time = timePicker.Value.ToShortTimeString();
        //label1.Text = formPopup.time;

        Label newLabel = new Label();
        newLabel.Text = formPopup.time;
        newLabel.Location = new System.Drawing.Point(205 + (100 * formTimeTable.CMonTime), 78);
        formTimeTable.CMonTime++;
        newLabel.Size = new System.Drawing.Size(100, 25);
        newLabel.ForeColor = System.Drawing.Color.White;
        thisParent.Controls.Add(newLabel);

        this.Close();
}

这是 Accept 按钮单击处理程序,它将标签放置在正确的位置。而变量CMonTime 会跟踪特定时间按钮被按下的次数。

public static int CMonTime = 0;
private void btnMonTime_Click(object sender, EventArgs e)
{
    formPopup f2 = new formPopup();
    f2.thisParent = this;
    f2.Show();      
}

这就是星期一的时间按钮单击处理程序中发生的事情。

但我如何知道实际点击了哪一天的时间按钮以正确放置时间戳标签?

就像点击星期二的时间按钮一样,时间戳应该显示在星期二的时间按钮旁边。

我尽量说清楚。

提前致谢。

【问题讨论】:

    标签: c# winforms label buttonclick


    【解决方案1】:

    您可以通过将 sender 参数转换为 Button 控件来获取被点击的按钮。 使用按钮的位置作为 formPopup 构造函数的参数

    private void btnMonTime_Click(object sender, EventArgs e)
    {
        var button = (Button)sender;
        formPopup f2 = new formPopup(button.Location);
        f2.thisParent = this;
        f2.Show();      
    }
    

    formPopup

    Point _buttonLocation;
    
    public frmPopup(Point buttonLocation)
    {
        _buttonLocation = buttonLocation;
    }
    

    然后使用按钮的位置来设置标签的位置

    private void btnAccept_Click(object sender, EventArgs e)
    {
        formPopup.time = timePicker.Value.ToShortTimeString();
    
        Label newLabel = new Label();
        newLabel.Text = formPopup.time;
        newLabel.Location = new Point(_buttonLocation.X + 100, _buttonLocation.Y);
    
        formTimeTable.CMonTime++;
        newLabel.Size = new System.Drawing.Size(100, 25);
        newLabel.ForeColor = System.Drawing.Color.White;
        thisParent.Controls.Add(newLabel);
    
        this.Close();
    }
    

    【讨论】:

      【解决方案2】:

      sender 是引发事件的对象。在您的情况下,它将是用户单击的按钮。

      【讨论】:

      • 您能详细说明一下吗?
      • 你不关注哪一点?
      【解决方案3】:

      由于这些控件组合重复出现,因此创建包含所需按钮和标签的 UserControl 可能会更容易。将 UserControl 视为由几个控件组成的小窗体,您可以根据需要多次将其放置在窗体上。它有自己的事件处理程序。这样,从技术上讲,只有一个按钮(不是七个)和一个事件处理程序。然后你可以在你的表单上输入UserControl 七次。

      还有其他方法可以避免重复代码,例如拥有一个事件处理程序并将其分配给所有七个按钮的点击事件。但是如果您想自己编辑按钮的布局,UserControl 将真正节省您的时间。你不想这样做七次。

      尝试一下:

      • 添加 > 新项目 > 用户控制。你会得到一个看起来像新表格的东西。向其中添加一些控件,就像向表单添加控件一样。然后使用名称(仅作为示例)MyUserControl.cs 保存它。
      • 构建项目。
      • 工具箱中将出现一个新的工具箱选项卡,您的控件将在那里。
      • 将您的控件拖到您的表单上,就像您将任何其他控件一样。

      More info on creating user controls

      【讨论】:

        猜你喜欢
        • 2017-06-04
        • 1970-01-01
        • 2021-05-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        相关资源
        最近更新 更多