繁体   English   中英

使用计时器时如何将多个按钮与其对应的标签链接起来? (Visual Studios 2015)

[英]How Do I Link Multiple Buttons With Their Corresponding Labels While Using A Timer? (Visual Studios 2015)

我从先前的问题中得到了答案,但这并不完全正确。 我希望button1button2做相同的事情,但事实并非如此。 我也想知道如何在代码中添加更多按钮(我总共需要4个按钮)。 我不知道是否有更简单的方法可以达到与我要尝试的结果相同的结果。 如果有,请向我解释。 我一直在努力使用c#(或者我不确定他们是否称Windows为Windows),因为它相对较新。 我正在尝试学习如何修复代码,但是这个答案不是我所熟悉的。 我通常使用Button btn = (Button)sender; 使用tmr.Tick += new EventHandler(timer_Tick); 这是我得到的答案(我对其进行了一些编辑,以便可能是我做错了地方)。

    public Form1()
    {
        InitializeComponent();
        this.SerCorrespondingButtons();
        this.SetCorrespondingInitialButtonInscriptions();
    }
    System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();
    private Dictionary<Button, Label> correspondingControls;
    private Dictionary<Button, string> initialButtonInscriptions;

    private List<string> sayingsList;
    private Button currentButton;

    private int i;
    private void SerCorrespondingButtons()
    {
        correspondingControls = new Dictionary<Button, Label>()
                                                              {
        {this.button1, this.label1},
        {this.button2, this.label2},
                                                              };
    }

    private void SetCorrespondingInitialButtonInscriptions()
    {
        this.initialButtonInscriptions = new Dictionary<Button, string>()
                                                              {
        {this.button1, this.button1.Text},
        {this.button2, this.button2.Text}
                                                              };
    }
    private void button2_Click(object sender, EventArgs e)
    {
        var button = sender as Button;
        if (this.currentButton != null)
        {
            this.currentButton.Enabled = true;
            this.currentButton.Text = this.initialButtonInscriptions[this.currentButton];
            this.currentButton.Click += this.button2_Click;
            tmr.Enabled = false;
            tmr.Tick -= timer2_Tick;
        }
        this.currentButton = button;
        var saying = this.correspondingControls[button];
        saying.Visible = true;
        button.Text = "Click To Hide Saying";
        button.Click -= button2_Click;
        button.Click += button2_Click2;
    }
    private void button2_Click2(object sender, EventArgs e)
    {
        var button = sender as Button;
        var saying = this.correspondingControls[button];
        saying.Visible = true;
        if (saying.Visible)
        {
            button.Enabled = false;
            saying.Visible = false;
            button.Text = "Reactivating in 5 seconds";
            tmr.Interval = 1000;
            button.Click -= button2_Click2;
            button.Click += button2_Click;
            this.i = 4;
            tmr.Tick += timer2_Tick;
            tmr.Enabled = true;
        }
    }
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (i != 0)
        {
            this.currentButton.Text = "Reactivating in " + i + " seconds";
            i -= 1;
        }
    }

它没有附带注释,因此,如果您可以在答案中添加注释/评论,将不胜感激。 如果您还可以给我有关我应该查找的内容的链接,我将不胜感激! 正如我说的,我尝试了其他方法,但它们并没有以这种方式接近。 谢谢高级!

Button_click是一个事件,在c#事件中进行通知对象有关任何修改或特定对象发生的事情。

该按钮是一个具有与Click相同的事件的对象
因此,如果您希望按钮执行相同的事件,则只需全部听一下,并对它们全部进行一次FUNCTION/OPERATION ,就可以参考此链接C#如何对多个按钮使用一键式事件处理程序

暂无
暂无

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

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