C#:重置按钮或返回到以前的空白

C#:重置按钮或返回到以前的空白,c#,winforms,button,timer,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); }

我已经多次尝试使这段代码正常工作,但我找不到解决方案。这是:

   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;
        }
    }
到目前为止,代码将执行以下操作:单击按钮时,将出现一个
语句/标签
。该按钮的名称将变为
“单击以隐藏并显示”
。再次单击时,标签将消失,文本将显示
“在(ii)秒内重新激活”
(ii是重新激活剩余的时间)


我想让它做的是当
ii=0
(或者当按钮延迟启动时)返回到
按钮两次单击事件。如果您有此问题的解决方案或其他方法,我将非常感谢。

为了方便和可重用性,请从事件中提取实际工作并将其放入函数中。然后,当计时器达到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;

    }
}

}

为了方便和可重用性,从事件中提取实际工作并将其放入函数中。然后,当计时器达到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;

    }
}

}

这是经过测试的,可以正常工作

    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。 使用变量存储初始按钮wo.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;
        }
    }
专用字典对应控件;
私人词典的缩写;
私人名单;
专用按钮当前按钮;
私人国际ii;
公共表格1()
{
初始化组件();
这个.SerCorrespondingButtons();
此.SetCorrespondingInitialButtonInscriptions();
}
私有void SerCorrespondingButtons()
{
对应的控件=新字典()
{
{this.buttone,this.sayingOne},
{this.buttonTwo,this.sayingtw},
};
}
私有void setcorrespondingitalbuttoninscriptions()对应
{
this.initialButtonScriptions=新字典()
{
{this.buttone,this.buttone.Text},
{this.buttonTwo,this.buttonTwo.Text}
};
}
私有无效按钮双击(对象发送者,事件参数e)
{
var按钮=发送方为按钮;
如果(this.currentButton!=null)
{
this.currentButton.Enabled=true;
this.currentButton.Text=this.initialButton脚本[this.currentButton];
this.currentButton.Click+=this.button两次单击;
tm2.Enabled=false;
tm2.滴答声-=计时器2_滴答声;
}
this.currentButton=按钮;
var saying=这个。对应的控件[按钮];
可见=真实;
button.Text=“单击以隐藏讲话”;
按钮。单击-=按钮两次单击;
按钮。单击+=按钮两次单击2;
}
私有无效按钮两次单击2(对象发送者,事件参数e)
{
var按钮=发送方为按钮;
var saying=这个。对应的控件[按钮];
可见=真实;
如果(表示可见)
{
按钮。已启用=错误;
可见=虚假;
button.Text=“5秒后重新激活”;
tm2.间隔=1000;
按钮。单击-=按钮两次单击2;
按钮。单击+=按钮两次单击;
这1.ii=4;
tm2.滴答声+=计时器2_滴答声;
tm2.Enabled=true;
}
}
私有无效计时器2_刻度(对象发送方,事件参数e)
{
如果(ii!=0)
{
this.currentButton.Text=“在“+ii+”秒内重新激活”;
ii-=1;
}
}

已测试且有效。

这是已测试且有效的

    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。 使用变量存储初始按钮wo.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;
        }
    }
专用字典对应控件;
私人词典的缩写;
私人名单;
专用按钮当前按钮;
私人国际ii;
公共表格1()
{
初始化组件();
这个.SerCorrespondingButtons();
此.SetCorrespondingInitialButtonInscriptions();
}
私有void SerCorrespondingButtons()
{
对应的控件=新字典()
{
{this.buttone,this.sayingOne},
{this.buttonTwo,this.sayingtw},
};
}
私有void setcorrespondingitalbuttoninscriptions()对应
{
this.initialButtonScriptions=新字典()
{
{this.buttone,this.buttone.Text},
{this.buttonTwo,this.buttonTwo.Text}
};
}
私有无效按钮双击(对象发送者,事件参数e)
{
var按钮=发送方为按钮;
如果(this.currentButton!=null)
{
this.currentButton.Enabled=true;
this.currentButton.Text=this.initialButton脚本[this.currentButton];
this.currentButton.Click+=this.button两次单击;
tm2.Enabled=false;
tm2.滴答声-=计时器2_滴答声;
}
this.currentButton=bu