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

 我来答
zeng121
2011-11-10 · 超过21用户采纳过TA的回答
知道答主
回答量:88
采纳率:0%
帮助的人:42.2万
展开全部
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 = "闹钟已经停止";
}
}
}
一个闹钟程序,和倒计时差不多。
追问
恩恩!!!太感谢了!!! 程序还能删减么?再稍微简单一点可以么。。。。
追答
那个还是比较完整的计算器,你可以只选其中的一些功能。
匿名用户

2011-11-10
展开全部
添加一个Timer组件就可以了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式