这是我的计时器
private void timer_tick(object sender, EventArgs e)
{
string timeNow = "";
timeNow = DateTime.Now.ToString("hh:mm") + " " + DateTime.Now.ToString("tt");
medicineAlarm();
}
private void medicineAlarm()
{
DataTable dt = new DataTable();
dt = database.getSchedule();
string AMTime;
string PMTime;
string NNTime;
foreach (DataRow row in dt.Rows)
{
AMTime = row["AMIntake"].ToString();
PMTime = row["PMIntake"].ToString();
NNTime = row["NNIntake"].ToString();
if (AMTime == timeNow || PMTime == timeNow || NNTime == timeNow)
{
MessageBox.Show("Drink Medicine");
}
}
}
即使我正在运行程序,如何显示消息?希望您能够帮助我。这是我的form_load
private void Form1_Load(object sender, EventArgs e)
{
timer.Interval = 1000;
timer.Tick += new EventHandler(this.timer_tick);
timer.Start();
答案 0 :(得分:0)
我认为您的代码完全没有编译,因为变量string timeNow = "";
是在事件处理程序中声明的,并且在您的方法体中不存在。您可能希望将其传递给类似
private void medicineAlarm(string timeNow)
{
然后你叫它
private void timer_tick(object sender, EventArgs e)
{
string timeNow = "";
timeNow = DateTime.Now.ToString("hh:mm") + " " + DateTime.Now.ToString("tt");
medicineAlarm(timeNow);
}
答案 1 :(得分:0)
medicineAlarm()
方法无法访问您在timeNow
事件处理程序中设置的timer_tick()
变量。我建议你把变量传递给:
private void timer_tick(object sender, EventArgs e)
{
string timeNow = DateTime.Now.ToString("hh:mm") + " " + DateTime.Now.ToString("tt");
medicineAlarm(timeNow);
}
private void medicineAlarm(string timeNow)
{
DataTable dt = database.getSchedule();
foreach (DataRow row in dt.Rows)
{
foreach(var label in new [] {"AM", "PM", "NN"})
if (row[label + "Intake"].ToString() == timeNow)
{
MessageBox.Show("Drink Medicine");
return;
}
}
}