我想要的是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#的新手。請善待。
你爲什麼這樣做?你想達到什麼目的?如果你解釋,可以給出一個更好的實現方法。 – Oded 2011-05-10 20:13:30
@已付 - 良好的通話。該程序非常簡單:在刷新時,它會查詢填充DataTable進行顯示的SQL DB。我想使用這個組合框和定時器來自動刷新重新定時器上的數據表,並且我希望在用戶選擇新的時間表(4小時,2小時,1小時,15分鐘)後立即更新定時器時間間隔。 – newuser 2011-05-10 20:16:40
你看過['SqlDependency'](http://msdn.microsoft.com/zh-cn/library/system.data.sqlclient.sqldependency.aspx)類嗎? – Oded 2011-05-10 20:19:16