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 = "闹钟已经停止"
}
}
}
程序的窗体内含有两个Timer控件,一个文本框控件,两个命令按钮控件,四个标签控件。其中Timer1的Interval属性设置为60000,也就是一分钟响应一次,Timer1的Interval属性设置为500,也就是每0.5秒响应一次。Label1显示剩余时间,Label2显示现在时间。代码如下:
Dim n As Integer
Dim t As String
Private Sub Command1_Click()'计时开始
If Text1.Text = "" Then
Exit Sub
End If
t = Val(Text1.Text)
Timer1.Enabled = True
Label1.Caption = "剩余时间:" &t &"分钟"
End Sub
Private Sub Command2_Click() '退出程序
End
End Sub
Private Sub Timer1_Timer()
n = n + 1
Label1.Caption = "剩余时间:" &t - n &"分钟"
If n = t Then
Beep
MsgBox "时间到", vbExclamation + vbOKOnly
Timer1.Enabled = False
n = 0
End If
End Sub
Private Sub Timer2_Timer()
Label2.Caption = "现在时间:" &Time
End Sub
Dim Ts As Integer, Tm As Integer, Th As IntegerDim StartT As Boolean
Private Sub Command1_Click()
If StartT = False Then
Command1.Caption = "暂停时间"
Else
Command1.Caption = "启动时间"
End If
StartT = Not StartT
End Sub
Private Sub Command2_Click()
StartT = False
Command1.Caption = "启动时间"
Ts = 0
Tm = 0
Th = 0
End Sub
Private Sub Command3_Click()
Th = Val(InputBox("输入小时"))
Tm = Val(InputBox("输入分钟"))
Ts = Val(InputBox("输入秒"))
End Sub
Private Sub Form_Load()
Command1.Caption = "启动时间"
Command2.Caption = "停止"
Command3.Caption = "设置时间"
Label1.Caption = "0:00:00"
Timer1.Enabled = True
Timer1.Interval = 1000
Ts = 0
Tm = 0
Th = 0
StartT = False
End Sub
Private Sub Timer1_Timer()
If StartT = True Then
Ts = Ts - 1
If Ts = -1 Then
Ts = 59
Tm = Tm - 1
If Tm = -1 Then
Tm = 59
Th = Th - 1
End If
End If
If Th = 0 And Tm = 0 And Ts = 0 Then
StartT = False
End If
End If
Label1.Caption = Th &":" &Format(Tm, "00") &":" &Format(Ts, "00")
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)