怎样用C#语言 做一个简单的有倒计时功能的小程序。求代码

2024-11-19 13:15:20
推荐回答(2个)
回答1:

using System;
using System.Collections;
using System.Timers;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace 计时器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

[DllImport("kernel32",EntryPoint="Beep")]
public extern static int Beep(int dwfreq,int dwduration);

private void numericUpDown3_ValueChanged(object sender, EventArgs e)
{
string hour, minute, second;
if (numericUpDown3.Value == 60)
{
numericUpDown3.Value = 0;
numericUpDown2.Value = Convert.ToInt32(numericUpDown2.Value) + 1;
int our = Convert.ToInt32(numericUpDown1.Value);
int niuit = Convert.ToInt32(numericUpDown2.Value);
int secon = Convert.ToInt32(numericUpDown3.Value);
if (our < 10)
{
hour = "0" + our.ToString();
}
else
{
hour = our.ToString();
}
if (niuit < 10)
{
minute = "0" + niuit.ToString() + ":";
}
else
{
minute = niuit.ToString() + ":";
}
if (secon < 10)
{
second = "0" + secon.ToString() + ":";
}
else
{
second = secon.ToString() + ":";
}
}
else
{
int our = Convert.ToInt32(numericUpDown1.Value);
int niuit = Convert.ToInt32(numericUpDown2.Value);
int secon = Convert.ToInt32(numericUpDown3.Value);
if (our < 10)
{
hour = "0" + our.ToString()+ ":" ;
}
else
{
hour = our.ToString()+ ":" ;
}
if (niuit < 10)
{
minute = "0" + niuit.ToString() + ":";
}
else
{
minute = niuit.ToString() + ":";
}
if (secon < 10)
{
second = "0" + secon.ToString();
}
else
{
second = secon.ToString();
}
}

label2.Text = hour + minute + second;

}

private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
string hour, minute, second;
if (numericUpDown2.Value == 60)
{
numericUpDown2.Value = 0;
numericUpDown1.Value = Convert.ToInt32(numericUpDown1.Value) + 1;
int our = Convert.ToInt32(numericUpDown1.Value);
int niuit = Convert.ToInt32(numericUpDown2.Value);
int secon = Convert.ToInt32(numericUpDown3.Value);
if (our < 10)
{
hour = "0" + our.ToString() + ":" ;
}
else
{
hour = our.ToString() + ":" ;
}
if (niuit < 10)
{
minute = "0" + niuit.ToString() + ":";
}
else
{
minute = niuit.ToString() + ":";
}
if (secon < 10)
{
second = "0" + secon.ToString();
}
else
{
second = secon.ToString();
}
}
else
{
int our = Convert.ToInt32(numericUpDown1.Value);
int niuit = Convert.ToInt32(numericUpDown2.Value);
int secon = Convert.ToInt32(numericUpDown3.Value);
if (our < 10)
{
hour = "0" + our.ToString() + ":";
}
else
{
hour = our.ToString() + ":";
}
if (niuit < 10)
{
minute = "0" + niuit.ToString() + ":";
}
else
{
minute = niuit.ToString() + ":";
}
if (secon < 10)
{
second = "0" + secon.ToString() ;
}
else
{
second = secon.ToString();
}
}
label2.Text = hour + minute + second;

}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
string hour, minute, second;
if (numericUpDown1.Value == 24)
{
numericUpDown1.Value = 0;
int our = Convert.ToInt32(numericUpDown1.Value);
int niuit = Convert.ToInt32(numericUpDown2.Value);
int secon = Convert.ToInt32(numericUpDown3.Value);
if (our < 10)
{
hour = "0" + our.ToString();
}
else
{
hour = our.ToString() ;
}
if (niuit < 10)
{
minute = "0" + niuit.ToString() + ":";
}
else
{
minute = niuit.ToString() + ":";
}
if (secon < 10)
{
second = "0" + secon.ToString() + ":";
}
else
{
second = secon.ToString() + ":";
}
}
else
{
int our = Convert.ToInt32(numericUpDown1.Value);
int niuit = Convert.ToInt32(numericUpDown2.Value);
int secon = Convert.ToInt32(numericUpDown3.Value);
if (our < 10)
{
hour = "0" + our.ToString() + ":";
}
else
{
hour = our.ToString() + ":";
}
if (niuit < 10)
{
minute = "0" + niuit.ToString() + ":";
}
else
{
minute = niuit.ToString() + ":";
}
if (secon < 10)
{
second = "0" + secon.ToString();
}
else
{
second = secon.ToString();
}

}

label2.Text = hour + minute +second ;

}

private void button1_Click(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Enabled = true;

}

private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
int hours = DateTime.Now.Hour;
int munit = DateTime.Now.Minute;
int second = DateTime.Now.Second;
long dat;

if ((int)numericUpDown1.Value > hours)
{

dat = (long)((int)numericUpDown1.Value - hours) * 3600 + ((int)numericUpDown2.Value - munit) * 60 + ((int)numericUpDown3.Value - second);
}
else
{
if ((int)numericUpDown2.Value > munit)
{

dat = (long)((int)numericUpDown1.Value - hours) * 3600 + ((int)numericUpDown2.Value - munit) * 60 + ((int)numericUpDown3.Value - second);
}
else
{
if ((int)numericUpDown3.Value >= second)
{

dat = (long)((int)numericUpDown1.Value - hours) * 3600 + ((int)numericUpDown2.Value - munit) * 60 + ((int)numericUpDown3.Value - second);
}
else
{
dat = (long)((int)numericUpDown1.Value + 24 - hours) * 3600 + ((int)numericUpDown2.Value - munit) * 60 + ((int)numericUpDown3.Value - second);
}
}
}
if (dat > 0)
{

label3.Text = "闹钟已经启动";
label4.Text = "剩余" + dat.ToString() + "秒";

}

if (dat == 0)
{
timer1.Enabled = false;
Beep(200, 500);
label4.Text = "时间已到";
}

}

private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
label3.Text = "闹钟已经停止";
}
}
}
一个闹钟程序,和倒计时差不多。

回答2:

添加一个Timer组件就可以了。