我想要的是有一个 ComboBox
,它在 SelectedIndexChanged
上更改 Timer.Interval
。我的代码基本上是这样的:
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# 的新手了。请善待。
最佳答案
一种方法是将 Time 对象声明为类的成员。然后您将能够在事件中访问它。
您还应该删除“throw new NotImplementedException();”来自你的事件,因为这条语句抛出异常
关于c# - 如何将计时器传递给事件处理程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5955846/