c# - 如何将计时器传递给事件处理程序?

标签 c# event-handling parameter-passing

我想要的是有一个 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/

相关文章:

C++:如何检查我的窗口是否即将关闭?

.net - 使用 ServiceLocator Bootstrapper 在 MVVM 应用程序中注册事件处理程序的正确位置?

c++ - 是否可以默认传递 "this"?

bash - 在执行 curl 获取的脚本时将参数传递给 bash

java - 如何在 JavaFX 中向 stackpane 上的同级发送事件

c - 如何通过 Windows 上的 system() 函数将换行符作为参数传递?

c# - 如何在 MVC 中运行控制台应用程序

c# - 在 C# 中找出谁锁定了网络驱动器上的文件

c# - WPF MVVM-简单登录到应用程序

c# - 如何将 ControlTemplate 转换为 WPF 中的 XAML 字符串?