2016-09-20 14 views
1

このコードスニペットを動作させるために複数回試行しましたが、解決策が見つかりません。ここでは、次のとおりです。ここでC#:ボタンをリセットするか、以前の無効に戻す

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イベントに戻ってください。あなたがこの問題やそれを行う別の方法の解決策を持っているなら、私は本当にそれを感謝するでしょう。

+0

「Tick」イベントハンドラの開始時にタイマーを停止し、最後にもう一度起動することはどうですか? –

+0

@UweKeim私はタイマーが問題だと思います。イベントハンドラのリンクです。これは役に立ちますか? –

答えて

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

基本的にはwhileと置き換えられます。 初期のbuttonTwo.Textを格納するために変数を使用します。これは、リセット時にボタンに表示するテキストです。

EDIT:

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

をテストし、作品:あなたは複数のラベルを持つ複数のボタンをしたいとDRYに違反したくない 場合は、ここでは一つの解決策です。

+0

すべてのボタンでこれを行う必要がありますか? –

+0

カスタムイベントを使用していない場合、私はそれを見て答えはイエスです。この理由は、buttonTwo_Click本体に3つのクラスインスタンスがあり、1つではないので、拡張メソッドを使用できないためです。 –

+0

返信ありがとうございます。私は今より多くの情報を得ました。 –

0

簡単に再利用できるように、実際の作業をイベントから取り出し、関数に入れてください。これはイベントによって使用され、タイマが0に達すると

編集:Ifの代わりにWhileも使用されました。

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

私にこれを見せていただきありがとうございます。これはクラスの割り当てのためのもので、私ができること全てを検索しました。 –

関連する問題