【问题标题】:C#: Reseting A Button or Going Back To A Previous VoidC#:重置按钮或返回到先前的空白
【发布时间】:2017-01-28 13:00:19
【问题描述】:

我一直在尝试多次尝试使这个 sn-p 代码工作,但我找不到解决方案。这里是:

   private void buttonTwo_Click(object sender, EventArgs e)
    {
        sayingTwo.Visible = true;
        buttonTwo.Text = "Click To Hide Saying";
        buttonTwo.Click += new EventHandler(buttonTwo_Click2);
    }
    System.Windows.Forms.Timer tm2 = new System.Windows.Forms.Timer();
    private void buttonTwo_Click2(object sender, EventArgs e)
    {
        if(sayingTwo.Visible == true)
        {
            buttonTwo .Enabled = false;
            sayingTwo.Visible = false;
            buttonTwo.Text = "Reactivating in 5 seconds";
            tm2.Interval = (1000);
            tm2.Tick += new EventHandler(timer2_Tick);
            tm2.Enabled = true;
        }
    }
    int ii = 4;
    private void timer2_Tick(object sender, EventArgs e)
    {
        while (ii != 0)
        {
            buttonTwo.Text = "Reactivating in " + ii + " seconds";
            ii -= 1;
        }
        if (ii == 0)
        {
            ii += 4;
            tm2.Enabled = false;
            buttonTwo.Enabled = true;
        }
    }

到目前为止,代码将执行以下操作:单击按钮时,将出现 saying/label。按钮的名称将更改为"Click To Hide Saying"。再次点击时,标签会消失,文字会显示"Reactivating in (ii) seconds"(ii是重新激活的剩余时间)。

我想要它做的是在ii = 0 (或按钮延迟到时)返回buttonTwo_Click 事件。如果您有解决此问题的方法或其他方法,我将不胜感激。

【问题讨论】:

  • 如何在Tick 事件处理程序的开头停止计时器并在最后重新启动它?
  • @UweKeim 我认为计时器是问题所在。它是事件处理程序的链接。这有帮助吗?

标签: c# winforms button timer


【解决方案1】:

为了方便和可重用性,将实际工作从事件中取出并放入函数中。这可以由事件使用,当计时器达到 0 时

编辑:你也有一段时间而不是如果。

public void Form1_Load(object sender, EventArgs e)
{
    tm2.Interval = 1000;
    tm2.Tick += timer2_Tick;
}

private void buttonTwo_Click(object sender, EventArgs e)
{
    DoClickWork();
}
System.Windows.Forms.Timer tm2 = new System.Windows.Forms.Timer();


private void DoClickWork()
{
    buttonTwo.Text = "Reactivating in 5 seconds";
    buttonTwo.Enabled = false;
    tm2.Enabled = true;
    sayingTwo.Visible = false;

}
int ii = 4;
private void timer2_Tick(object sender, EventArgs e)
{
    if (ii != 0)
    {
        ii -= 1;
        buttonTwo.Text = "Reactivating in " + ii + " seconds";
    }
    if (ii == 0)
    {
        ii += 4;
        tm2.Enabled = false;
        buttonTwo.Enabled = true;
        buttonTwo.Text = "Click To Hide Saying";
        sayingTwo.Visible = true;

    }
}

}

【讨论】:

  • 谢谢你给我看这个。这是一个课堂作业,我搜索了所有我能找到的东西。
【解决方案2】:

这是经过测试并且有效的。

    private string text;
    public Form1()
    {
        InitializeComponent();
        text = this.buttonTwo.Text;
    }


    private void buttonTwo_Click(object sender, EventArgs e)
    {
        sayingTwo.Visible = true;
        buttonTwo.Text = "Click To Hide Saying";
        buttonTwo.Click -= buttonTwo_Click2;
        buttonTwo.Click += buttonTwo_Click2;
    }
    private void buttonTwo_Click2(object sender, EventArgs e)
    {
        if (sayingTwo.Visible == true)
        {
            buttonTwo.Enabled = false;
            sayingTwo.Visible = false;
            buttonTwo.Text = "Reactivating in 5 seconds";
            tm2.Interval = 1000;
            buttonTwo.Click -= buttonTwo_Click2;
            tm2.Tick += timer2_Tick;
            tm2.Enabled = true;
        }
    }
    int ii = 4;
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (ii != 0)
        {
            buttonTwo.Text = "Reactivating in " + ii + " seconds";
            ii -= 1;
        }
        else
        {
            tm2.Enabled = false;
            tm2.Tick -= timer2_Tick;
            buttonTwo.Enabled = true;
            this.buttonTwo.Text = this.text;
            ii += 4;
        }
    }

基本上,您将 while 替换为 if ,如其他答案所述。 使用变量来存储您的初始 buttonTwo.Text,因为我想这是您希望在重置时出现在按钮上的文本。

编辑: 如果您想要具有多个标签的多个按钮并且不想违反 DRY,这是一种解决方案:

    private Dictionary<Button, Label> correspondingControls;
    private Dictionary<Button, string> initialButtonInscriptions;

    private List<string> sayingsList;
    private Button currentButton;

    private int ii;
    public Form1()
    {
        InitializeComponent();
        this.SerCorrespondingButtons();
        this.SetCorrespondingInitialButtonInscriptions();
    }

    private void SerCorrespondingButtons()
    {
        correspondingControls = new Dictionary<Button, Label>()
                                                                  {
            {this.buttonOne, this.sayingOne},
            {this.buttonTwo, this.sayingTwo},
                                                                  };
    }

    private void SetCorrespondingInitialButtonInscriptions()
    {
        this.initialButtonInscriptions = new Dictionary<Button, string>()
                                                                  {
            {this.buttonOne, this.buttonOne.Text},
            {this.buttonTwo, this.buttonTwo.Text}
                                                                  };
    }

    private void buttonTwo_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.buttonTwo_Click;
            tm2.Enabled = false;
            tm2.Tick -= timer2_Tick;
        }

        this.currentButton = button;
        var saying = this.correspondingControls[button];
        saying.Visible = true;
        button.Text = "Click To Hide Saying";
        button.Click -= buttonTwo_Click;
        button.Click += buttonTwo_Click2;
    }
    private void buttonTwo_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";
            tm2.Interval = 1000;
            button.Click -= buttonTwo_Click2;
            button.Click += buttonTwo_Click;
            this.ii = 4;
            tm2.Tick += timer2_Tick;
            tm2.Enabled = true;
        }
    }
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (ii != 0)
        {
            this.currentButton.Text = "Reactivating in " + ii + " seconds";
            ii -= 1;
        }
    }

经过测试并且有效。

【讨论】:

  • 我必须为每个按钮都这样做吗?
  • 对于我的看法,如果您不使用自定义事件,答案是肯定的。这样做的原因是,您不能使用扩展方法,因为您在 buttonTwo_Click 正文中有三个类实例,而不是一个。
  • 好的。感谢您的答复。我现在更了解情况了。
  • 其实不用自定义事件也可以。查看我的编辑。
  • 感谢您为此付出的努力。我很感激。
猜你喜欢
  • 1970-01-01
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多