2017-03-24 89 views
0

我的主應用程序中有定時器功能,它充當定時會話。但是,我想更改定時器間隔以及通過子窗體打開和關閉它的功能,但不能使用另一個窗體中的定時器功能元素來更改設置。以下是我的代碼片段。任何提示或例子將不勝感激。如何更改c主窗體的子窗體的計時器設置#

嘗試: 主窗體

private void Booyaa_Load(object sender, EventArgs e) 
     { 
      BooyaaTimer.Interval = 45 * 60 * 1000); // 45 mins 
      BooyaaTimer.Tick += new EventHandler(BooyaaTimer_Tick); 
      BooyaaTimer.Start(); 
      if (!Properties.Settings.Default.SettingShutdown) 
      { 
       MessageBox.Show("Not properly shut down"); 
       GetPass pass = new GetPass(); 
       DialogResult result = pass.ShowDialog(); 
       if (result == DialogResult.OK) { 

        Properties.Settings.Default.SettingShutdown = true; 
       Properties.Settings.Default.Save(); 
      } 
      else 
      { 
       Close(); 
      } 
      } 

private void BooyaaTimer_Tick(object sender, EventArgs e) 
     { 
      MessageBox.Show("TIME IS UP"); 

      GetPass pass = new GetPass(); 
       DialogResult result = pass.ShowDialog(); 
       if (result == DialogResult.OK) 
       { 

       Show(); 
       Properties.Settings.Default.SettingShutdown = true; 
       Properties.Settings.Default.Save(); 
      } 
       else 
      { 

       BooyaaTimer.Start(); 
       Properties.Settings.Default.SettingShutdown = false; 
       Properties.Settings.Default.Save(); 
       Hide(); 
      } 

      } 

Timer控件形成

public object BooyaaTimer { get; private set; } 



      private void btn_confirm_Click(object sender, EventArgs e) 
      { 


BooyaaTimer.Interval = Int32.Parse(textBox1.Text); // gives error on interval 

      } 
+1

的'btn_confirm_Click'代碼是不完整的創建。 –

+0

@JohnnyMopp我的觀點是我無法繼續它,因爲我無法使用主窗體中的區間函數。 –

+0

什麼是定時器?爲什麼'BooyaaTimer'是一個'object'而不是'System.Windows.Forms.Timer'對象?請閱讀如何創建[mcve]。 –

回答

1

我打算做一個猜測。該定時器在主窗體

public partial class Form1 : Form { 
    Timer BooyaaTimer = new Timer(); // Or this is created in Designer 

    void SomeFunctionThatCreatesTheOtherForm() { 
     TimerControlsForm form2 = new TimerControlsForm(); 
     // Pass the timer to form2 
     form2.BooyaTimer = BooyaTimer; 
     form2.ShowDialog(); 
    } 
} 

而其他形式的

public partial class TimerControlsForm : Form { 
    // This has to be a Timer object 
    public Timer BooyaTimer {get; set;} 

    private void btn_confirm_Click(object sender, EventArgs e) { 
     BooyaaTimer.Interval = Int32.Parse(textBox1.Text); 
    } 
} 
+0

我設法解決了錯誤通過:public static System.Windows.Forms.Timer BooyaaTimer {get;內部設置;}但我仍然在BooyaaTimer.Interval = Int32.Parse(textBox1.Text);當我輸入數字並按下btn_confirm時 –

+0

錯誤是什麼? –

+0

未處理的異常 –

相關問題