C#怎么实现倒计时的功能?
1个回答
展开全部
刚好有空 给你写了一个 代码:using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;namespace WinForm_test_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int hour = 12;
int minute = 0;
int second = 0;
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Func);
thread.Start();
} private void Func()
{
while (hour > 0)
{
this.Invoke((EventHandler)delegate {
if (second < 0)
{
second = 59;
minute--; }
if (minute < 0)
{
minute = 59;
hour--;
}
label1.Text = "剩余时间:" + hour.ToString() + "小时 " + minute.ToString() + "分钟 " + second.ToString() + "秒";
});
Thread.Sleep(1000);
second--;
}
} }
}
有不明白的地方请追问.
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;namespace WinForm_test_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} int hour = 12;
int minute = 0;
int second = 0;
private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(Func);
thread.Start();
} private void Func()
{
while (hour > 0)
{
this.Invoke((EventHandler)delegate {
if (second < 0)
{
second = 59;
minute--; }
if (minute < 0)
{
minute = 59;
hour--;
}
label1.Text = "剩余时间:" + hour.ToString() + "小时 " + minute.ToString() + "分钟 " + second.ToString() + "秒";
});
Thread.Sleep(1000);
second--;
}
} }
}
有不明白的地方请追问.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询