2011-05-10 45 views
1

我想要的是ComboBox,其中SelectedIndexChanged更改爲Timer.Interval。我的代碼基本上是這樣的:如何將定時器傳遞給EventHandler?

public Form1() 
{ 
    InitializeComponent(); 
    Timer AutoRefresh = new Timer(); 
    AutoRefresh.Tick += new EventHandler(AutoRefresh_Tick); 
    var RefreshIntervals = new[] { "4 hours", "2 hours", "1 hour", "15 minutes", "10 seconds" }; 
    comboBox1.DataSource = RefreshIntervals; 
    comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged); 
} 

void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (AutoRefresh.Enabled == true) 
     AutoRefresh.Enabled = false; 
    if (comboBox1.SelectedText == "4 hours") 
     AutoRefresh.Interval = 14400000; 
    else if (comboBox1.SelectedText == "2 hours") 
     AutoRefresh.Interval = 7200000; 
    else if (comboBox1.SelectedText == "1 hour") 
     AutoRefresh.Interval = 3600000; 
    else if (comboBox1.SelectedText == "15 minutes") 
     AutoRefresh.Interval = 900000; 
    else if (comboBox1.SelectedText == "10 seconds") 
     AutoRefresh.Interval = 10000; 
    AutoRefresh.Enabled = true; 
} 

現在,很明顯這不起作用,因爲comboBox1_SelectedIndexChanged()沒有一個Timer變量的引用。

如何修改我的代碼以將AutoRefresh轉換爲comboBox1_SelectedIndexChanged()

大概是個好時機,指出我仍然是C#的新手。請善待。

+0

你爲什麼這樣做?你想達到什麼目的?如果你解釋,可以給出一個更好的實現方法。 – Oded 2011-05-10 20:13:30

+0

@已付 - 良好的通話。該程序非常簡單:在刷新時,它會查詢填充DataTable進行顯示的SQL DB。我想使用這個組合框和定時器來自動刷新重新定時器上的數據表,並且我希望在用戶選擇新的時間表(4小時,2小時,1小時,15分鐘)後立即更新定時器時間間隔。 – newuser 2011-05-10 20:16:40

+1

你看過['SqlDependency'](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldependency.aspx)類嗎? – Oded 2011-05-10 20:19:16

回答

1

SqlDependency類可用於工作良好可見你,根據你所做的評論:

...該程序...查詢填充數據表的SQL DB顯示。我想用這個組合框和定時自動刷新數據表...

0

您需要將計時器放入課程中的field

2

一種方法可能是將Time對象聲明爲類的成員。然後你就可以在事件中訪問它。

同樣你應該刪除'throw new NotImplementedException();'從你的事件,因爲該語句拋出一個異常

2

提取某個字段中的本地varible形式構造和現在計時器會在處理器

Timer AutoRefresh; 
public Form1() 
{ 
    InitializeComponent(); 
AutoRefresh = new Timer(); 
    AutoRefresh.Tick += new EventHandler(AutoRefresh_Tick); 

resh.Interval = 10000; 
    AutoRefresh.Enabled = true; 

}

+0

我可以發誓,我試過,VS2010不會讓我建立... – newuser 2011-05-10 20:26:41

+0

你可能試圖在構造函數之外初始化它。每當你遇到構建錯誤,**閱讀**! – SLaks 2011-05-10 20:32:44

0

我發現一個錯誤,你的代碼

void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    throw new NotImplementedException(); // remove this line 
+0

我編輯了出來。 :) – newuser 2011-05-10 20:50:32

0
namespace WindowsFormsApplication1 
{ 
    using System; 
    using System.Windows.Forms; 

    public partial class Form1 : Form 
    { 
     /// <summary> 
     /// The default constructor. 
     /// I used the designer, so the InitializeComponent method contains the timer and combobox initialization. 
     /// </summary> 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     /// <summary> 
     /// Occurs when the combo box selection changes. 
     /// </summary> 
     /// <param name="sender">The sender object, i.e., the combobox</param> 
     /// <param name="e">The event arguments.</param> 
     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      if (autoRefresh.Enabled == true) 
      { 
       autoRefresh.Enabled = false; 
      } 

      // so I can easily change the time scale when debugging, I'm using a multiplier 
      const int multiplier = 10000; 

      // notice I'm using SelectedItem because the event triggered was index changed, not text changed 
      var selectedInterval = comboBox1.SelectedItem.ToString(); 

      // if a valid entry wasn't selected, then we'll disable the timer 
      var enabled = true; 

      switch (selectedInterval) 
      { 
       case "4 hours": 
        autoRefresh.Interval = 1440 * multiplier; 
        break; 
       case "2 hours": 
        autoRefresh.Interval = 720 * multiplier; 
        break; 
       case "1 hour": 
        autoRefresh.Interval = 360 * multiplier; 
        break; 
       case "15 minutes": 
        autoRefresh.Interval = 90 * multiplier; 
        break; 
       case "10 seconds": 
        autoRefresh.Interval = 1 * multiplier; 
        break; 
       default: 
        // we didn't choose a valid entry, or picked blank line 
        enabled = false; 
        break; 
      } 

      autoRefresh.Enabled = enabled; 
     } 

     /// <summary> 
     /// Occurs every time the timer reaches its interval 
     /// </summary> 
     /// <param name="sender">The sender object, i.e., the timer.</param> 
     /// <param name="e">The event arguments.</param> 
     private void AutoRefresh_Tick(object sender, EventArgs e) 
     { 
      // to ensure the next timer triggers at the desired interval 
      // stop the timer while doing the operations in this method (they could be lengthy) 
      // and then restart the timer before exiting 
      autoRefresh.Enabled = false; 

      // give a visual indicator of the timer ticking. 
      // Here is where you would do your DB operation. 
      MessageBox.Show(string.Format("Hello! time={0}:{1}", DateTime.Now.ToLongTimeString(), DateTime.Now.Millisecond)); 

      autoRefresh.Enabled = true; 
     } 
    } 
} 
0

我做了一些重構和發現一個錯誤在代碼 看看這個方法是好的工作在我的項目

void comboBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (AutoRefresh.Enabled) 
      AutoRefresh.Enabled = false; 
     var selectedItem = comboBox1.SelectedItem.ToString(); 

     switch (selectedItem) 
     { 
      case "4 hours": 
       AutoRefresh.Interval = 14400000; 
       break; 
      case "2 hours": 
       AutoRefresh.Interval = 7200000; 
       break; 
      case "1 hour": 
       AutoRefresh.Interval = 3600000; 
       break; 
      case "15 minutes": 
       AutoRefresh.Interval = 900000; 
       break; 
      case "10 seconds": 
       AutoRefresh.Interval = 10000; 
       break; 
     } 
     AutoRefresh.Enabled = true; 
    } 

你應該使用SelectedItem屬性保證SelectedText