我想要的是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()
传递给{{1}}?
可能是指出我仍然是C#新手的好时机。请善待。
答案 0 :(得分:2)
一种方法是将Time对象声明为类的成员。然后你就可以在活动中访问它了。
你也应该删除'throw new NotImplementedException();'来自您的事件,因为此语句抛出异常
答案 1 :(得分:2)
从字段中的构造函数中提取局部变量,现在计时器将在处理程序中可见
Timer AutoRefresh;
public Form1()
{
InitializeComponent();
AutoRefresh = new Timer();
AutoRefresh.Tick += new EventHandler(AutoRefresh_Tick);
resh.Interval = 10000;
AutoRefresh.Enabled = true;
}
答案 2 :(得分:1)
基于您所做的评论,SqlDependency
课程可能适合您:
...程序...查询填充DataTable进行显示的SQL DB。我想使用这个comboBox和Timer来自动刷新DataTable ......
答案 3 :(得分:0)
您需要将计时器放入班级中的field。
答案 4 :(得分:0)
我在你的代码中发现了一个错误
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
throw new NotImplementedException(); // remove this line
答案 5 :(得分: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;
}
}
}
答案 6 :(得分: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属性insteed SelectedText