2016-05-27 3 views
1

カウントダウンがあり、メインフォームのラベルに出力されます。メインフォームから子フォームにカウントダウンします他のすべてのウィンドウ(ゲーム用)私は子供のラベルのカウントダウンを更新することができません。私はポップアウトボタンを押すと、私は2番目になる。親ラベルからタイマーカウントダウンを取得

このメインフォーム上

private void tmrMain_Tick(object sender, EventArgs e) 
    { 
     TimerSeconds = TimerSeconds - 1; 
     if (TimerSeconds == 0) 
     { 
      tmrMain.Stop(); 
      if (chbxPlaySound.Checked) 
      { 
       System.Media.SystemSounds.Exclamation.Play(); 
      } 
      if (chbxRepeat.Checked) 
      { 
       btnStartTimer.PerformClick(); 
      } 
     } 

     string dis_seconds = Convert.ToString(TimerSeconds); 
     // 
     // lblTimer is the label that shows the countdown seconds 
     // 
     lblTimer.Text = dis_seconds; 

    } 

これは私のポップですアウトボタンメインフォーム上の

private void btnPopOut_Click(object sender, EventArgs e) 
    { 
     PopTimer ShowTimer = new PopTimer(TimerSeconds); 
     ShowTimer.Show(this); 
    } 

これは私の子フォーム

public partial class PopTimer : Form 
{ 
    public PopTimer(int countTimer) 
    { 
     InitializeComponent(); 
     //string dis_seconds = Convert.ToString(TimerSeconds); 
     lblPopTimer.Text = Convert.ToString(countTimer); ; 
    } 
} 

答えて

1

あなたPopTimerフォームに必要ですアクセス可能なメソッド、次のようなもの:

あなたのTickイベントに続いて

private PopTimer showTimer = null; 

private void btnPopOut_Click(object sender, EventArgs e) 
{ 
    showTimer = new PopTimer(timerSeconds); 
    showTimer.Show(this); 
} 

public void SetCountdown(int value) { 
    lblPopTimer.Text = value.ToString(); 
} 

は、その後、あなたのPopTimerがアクセス可能なので、宣言を移動する必要がある

if (showTimer != null) { 
    showTimer.SetCountdown(timerSeconds); 
} 
+0

は今完璧な作品、ありがとう – Manvaril