Я хочу иметь 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 #. Пожалуйста, будьте добры.