C#:重新设置按钮或返回上一个虚空

时间:2016-09-20 14:32:04

标签: c# winforms button timer

我一直在尝试多次尝试使这段代码工作,但我无法找到解决方案。这是:

   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是重新激活时剩余的时间)。

我希望它做的是在buttonTwo_Click时(或按钮延迟时间)返回ii = 0事件。如果您有解决此问题的方法或其他方式,我会非常感激。

2 个答案:

答案 0 :(得分:0)

为了方便和重复使用,请将实际工作从事件中取出并将其置于函数中。然后,事件可以使用,当计时器命中0时

编辑:你有一个而不是一个If。

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;

    }
}

}

答案 1 :(得分:0)

这是经过测试和运作的。

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

基本上,如果正如其他答案所述,则替换为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;
        }
    }

经过测试和工作。