如何用C#制作一个简单的倒计时器,就是从60一秒一秒的减到0位置,求详细的代码,谢了

作者&投稿:钟姬 (若有异议请与网页底部的电邮联系)
怎样用C#语言 做一个简单的有倒计时功能的小程序。求代码~

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 = "闹钟已经停止";
}
}
}
一个闹钟程序,和倒计时差不多。




最后一点,还在不会就问我。

Option Explicit
Private Sub Command1_Click()
If Command1.Caption = "开始" Then
If IsNumeric(Text1) Then
Text1 = 60 * Val(Text1) '化为秒,给你用分钟数演示
Command1.Caption = "结束"
Timer1.Interval = 1000
End If
Else
Command1.Caption = "开始"
Timer1.Interval = 0
If Text1 <> 0 Then Text1 = Text1 / 60
End If
End Sub
Private Sub Form_Load()
Frame1.Caption = "请输入倒计时的分钟数"
Text1 = ""
Command1.Caption = "开始"
End Sub
Private Sub Timer1_Timer()
If Command1.Caption = "开始" Then Exit Sub
Text1 = Text1 - 1
If Text1 <= 0 Then
Timer1.Interval = 0
Command1.Caption = "开始"
End If
End Sub


计算机C语言有什么用啊??
用处:C语言是一种计算机程序设计语言。它可以作为系统设计语言,编写工作系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。特征:1、C语言是高级语言。它把高级语言的基本结构和语句与低级语言的实用性结合起来。C 语言可以像汇编语言一样对位、字节和地址进行操作,而这三者是计算...

如何用c语言编一个成绩查询系统?
如果你是要做一个非常专业的商业软件,这个其实不太适合用C语言来编写。你可以考虑其他高级语言如java,c#,etc.如果你仅仅是想在功能上实现成绩查询,而且数据量不是那种特别大,比如千万,上亿级别的,用C语言以二进制文件读写的方式完全可以轻松实现。下面说说大概的思路:1.你可以定义一个结构体来描述...

c语言有什么用
“项目驱动式”教学就是以项目为目的,以c语言理论教学为过程,最终能用c语言设计项目,实现项目的要求。“项目驱动式”教学的关键在于培养学生“如何做什么”和“可以干什么”。一个项目就是一个工程,在“项目驱动式”教学中,首先应该让学生简单了解什么是软件工程思想,其次在c语言理论教学过程中,让学生懂得面向对象的...

如何用c语言在windows平台上开发php extension
02.vi int_ext.c 03.#PHP_FE(confirm_int_ext_compiled, NULL) 面添加 PHP_FE(int_ext, NULL)添加:04.1. zend_function_entry int_ext_functions[] = { 05.2. PHP_FE(confirm_int_ext_compiled, NULL) \/* For testing, remove later. *\/ 06.3. PHP_FE(int_ext, NULL)...

中职数学 求解释 排列与组合的 意思与 区别 题何时用P 何时用 C...
P就是排列 是有顺序的 例如从4人中选1个班长和1个团支书 拿A,B这两个人看 职位调换是结果不同的 C就是组合 是无顺序的 例如4人选2个作为代表 那么AB的职位一样 调换无意义

如何用C\/C++开发一个web应用(Windows)?
如果想让界面效果更炫,那就用WPF让Winform在界面美化上更上一层楼。 但它会慢一点。如果对性能要求高或者操作系统底层使用C++或者直接C C++或者MFC,性能很好,但是开发难度比c#高。 如果直接用C,就比较难了。 优点是运行速度快,兼容性好。 除了汇编 C 和 C++ 应该是最快的。QT Qt是一个基于...

如何用C语言实现定时何延时功能
C语言中的两个函数 一个是 <windows.h> 库下的 Sleep()效果为当前进程等待 Sleep(x) 毫秒 可以实现延时功能 另一个是 下的 time()效果为返回当前时间,配合循环可以实现定时功能 以下是一个例子:\/\/刚写完#include<stdio.h>#include<windows.h>#includeint main(){printf("定时程序2秒后开启...

c语言如何设置断点,有何用处,谢了
int main(void){ printf("hello .\\n");printf("hello world.\\n");return 0;} 如果这个程序在第一个printf语句后设置断点就只输出hello,如果在第二个printf语句后设置断点,则两个printf后的语句都输出。2、将光标移动到想设置断点的地方,按一下F9键即可,或者可以用鼠标左键点击小手图标。当想...

井号在C语言中有何作用?
宏定义是C语言中非常常见的一种预处理器指令,用于定义一些常量或函数宏,以便于在后续的代码中使用。它的语法格式为:define 宏名 宏值 例如,我们可以使用宏定义来定义一个常量:define PI 3.1415926 在定义了宏之后,我们就可以在后续的代码中使用宏名来代替宏值,如下所示:double area = PI * ...

怎么用C语言的scanf实现一次输入若干个数据
根据需要一般选择如下3种方法:1,已知数量,且个数较少:scanf("%d%d%d",&a&b&c);2,已知数量,但数量较多:for(int i=0;i<n;i++)scanf("%d",&a[i]);3,不知道数量 int i=0;while(scanf("%d",&a[i])){ i++;}

黄浦区13136971999: 用C#怎么做一个倒计时器 -
农爽京制: (1)首先创建两个窗体,在Form1窗体上添加一个Button按钮和一个timer组件,用来执行倒计时功能;在Form2中添加一个Label控件,用于显示倒计时. (2)在Form2窗体中定义一个公共变量curr_time,用于为Label控件赋值,

黄浦区13136971999: 用c#怎么做一个倒计时的定时器 -
农爽京制: DateTime dtStart; private void button1_Click(object sender, EventArgs e) { int mins = Convert.ToInt32(textBox1.Text.Trim()); dtStart = DateTime.Now.AddMinutes(mins); timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { ...

黄浦区13136971999: C#怎么倒计时? -
农爽京制: private void button2_Click(object sender, EventArgs e) { Thread th = new Thread(new ThreadStart(StartTime)); th.IsBackground = true; th.Start(); } //跨线程更改Lable的值 delegate void DdShowTime(int times); private void ShowLabelTime(int ...

黄浦区13136971999: 如何用C#制作一个简单的倒计时器,就是从60一秒一秒的减到0位置,求详细的代码,谢了 -
农爽京制: Option ExplicitPrivate Sub Command1_Click() If Command1.Caption = "开始" Then If IsNumeric(Text1) Then Text1 = 60 * Val(Text1) '化为秒,给你用分钟数演示 Command1.Caption = "结束" Timer1.Interval = 1000 End If Else Command1....

黄浦区13136971999: 用C#制作一个计时器,如下图 -
农爽京制: 详细见附件using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace ...

黄浦区13136971999: 用C#窗体做一个简单的可以让用户自定义倒计时时间的程序 -
农爽京制: private DateTime alarmTime; //线程运行标志位,退出前需要设成flase;private bool runFlag; private double sencends; private void btnStart_Click(object sender, EventArgs e){if (runFlag){//已经启动计时器,防重入return;}//获取设...

黄浦区13136971999: 初学者,请问如何用C#编写一个倒计时程序? -
农爽京制: 用timer控件

黄浦区13136971999: 怎样用C#语言 做一个简单的有倒计时功能的小程序.求代码 -
农爽京制: 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 ...

黄浦区13136971999: C#如何做一倒计时表 (分钟 :秒)
农爽京制: 用一个Timer,在Timer启动前记录下一个开始时间,然后Timer每一秒钟启动一次,每次触发Timer的时候用5分钟-(当前时间-开始时间)就得出你那个倒计时的 4:59- 4:58- 然后Timer再判断一下如果这个值=0了就停止程序

黄浦区13136971999: 用C#编写简单计时器程序,急用 -
农爽京制: private void ShowTime() { System.Windows.Forms.Timer timer = new Timer();//创建定时器 timer.Interval = 100;//定时器的周期 timer.Tick+=new EventHandler(timer_Tick);//定时器的的委托事件 timer.Start();//定时器开始 } private void timer_Tick(object sender, EventArgs e) { label1.Text = DateTime.Now.ToString(); }

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网