如何知道在Winforms C#中单击了哪个按钮? [英] How to know which button was clicked in winforms c#?

查看:78
本文介绍了如何知道在Winforms C#中单击了哪个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这很难解释.

我正在处理基本的时间表表格.

I'm working on a basic time table form.

我有7个按钮,分别命名为 btnMonTime btnTueTime 等,直到一周的 btnSunTime 为止.现在,每次单击按钮时,都会打开一个弹出窗口(winform),使用户可以通过 dateTimePicker 控件选择特定时间.时间被解析为字符串并存储.弹出窗口上有一个 Accept 按钮,按下该按钮时,弹出窗口将关闭,并在特定日期旁边贴上一个标签,指出时间.

I have 7 buttons, named btnMonTime, btnTueTime and so on till btnSunTime based on the days of the week. Now on each button click, a pop up window (winform) opens which lets the user select a certain time through a dateTimePicker control. The time is parsed into a string and stored. There is an Accept button on the popup which when pressed, the popup closes and a label beside the particular day stating the time is posted.

`

现在我知道如何在特定的一天这样做,但问题是我只有一个函数来执行此标签创建.但是我如何知道单击了哪个时间"按钮以将其放置在正确的位置?

Now I know how to do it for one particular day, but the thing is that I have one single function doing this label creating. But how do I know which Time button was clicked to place it at the right place?

这是我能想到的代码:

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 可以跟踪特定 Time 按钮被按下了多少次.

This is the Accept button click handler which places the label at the right place. Whereas the variable CMonTime keeps track of how many times a particular Time button was pressed.

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

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

And this is what is happening inside the Monday's Time button click handler.

但是我怎么知道实际单击了哪一天的时间按钮以正确放置时间戳标签?

But how can I know which day's Time button was actually clicked for proper placement of the timestamp label?

就像单击周二的时间"按钮一样,时间戳应显示在周二"的时间"按钮旁边.

Like if Tuesday's Time button would be clicked, the timestamp should be displayed beside the Time button for Tuesday.

我试图尽可能清楚.

谢谢.

推荐答案

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

You can get the button that was clicked by casting the sender parameter as a Button control. Use the button's location as a parameter for your formPopup constructor

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

formPopup

formPopup

Point _buttonLocation;

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

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

Then use the button's location to set your label's location

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

这篇关于如何知道在Winforms C#中单击了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆