我想做一个倒计时,已经会做点,但是想这样显示,请会的人帮帮忙

gene31415926 2010-01-17 05:27:36
我做的是默认时间是10秒钟,然后把空间TIMER的属性INTERVAL改为1000,然后运行后以每秒减1的速度显示,然后到0时,控件timer停止运行,同时输出文本框信息“时间到!”
private void timer1_Tick(object sender, EventArgs e)
{
int i = 10;
if (i == 0)
{
this.timer1.Stop();
MessageBox.Show("时间到!");
}
label1.Text = (i--).ToString();

}



上面显示的结果是10,9,8,7……0


但是我想这样显示
00:00:10
00:00:09
00:00:08
……
00:00:00

请高手帮忙解决这个问题,感激不尽!!!!



...全文
102 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nixiang12 2010-01-17
  • 打赏
  • 举报
回复
打酱油的 路过
hhc123 2010-01-17
  • 打赏
  • 举报
回复
...............


private void button1_Click(object sender, EventArgs e)
{

this.timer1.Start();

}
TimeSpan ts = new TimeSpan(100000000);
private void timer1_Tick(object sender, EventArgs e)
{
if (!ts.Equals(new TimeSpan(0)))
{
ts = ts.Subtract(new TimeSpan(10000000));
this.label1.Text += ts.ToString() + "\n";


}
else
{
this.timer1.Stop();
}

}
王子样 2010-01-17
  • 打赏
  • 举报
回复
计时之类的都用 TimeSpan 就可以了。
ouc_ajax 2010-01-17
  • 打赏
  • 举报
回复
private int elpseTime = 20;
System.Timers.Timer timer;

private void button1_Click(object sender, EventArgs e)
{
timer = new System.Timers.Timer(1000);
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Start();
}

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Invoke(new EventHandler(SetTextValue));
}

private void SetTextValue(object sender, EventArgs args)
{
if (this.elpseTime > 0)
{

this.label1.Text = new TimeSpan((elpseTime--) * 1000).ToString();
}
else
{
this.label1.Text = "火箭发射";
timer.Stop();
}
}
gene31415926 2010-01-17
  • 打赏
  • 举报
回复
有谁知道在WINforms窗体里如何写代码呢(只要用一个label显示时间格式的倒计时)
gene31415926 2010-01-17
  • 打赏
  • 举报
回复
楼上的好牛啊,不知道只用一个LABEL标签在win窗体里怎么显示,我来试试看
hhc123 2010-01-17
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication47
{
class Program
{
static void Main(string[] args)
{
TimeSpan ts = new TimeSpan(100000000);

while(!ts.Equals(new TimeSpan(0)))
{
Thread.Sleep(1000);
ts= ts.Subtract(new TimeSpan(10000000));
Console.WriteLine(ts.ToString());

}
Console.WriteLine("火箭发射");
Console.ReadKey();
}

}
}

hhc123 2010-01-17
  • 打赏
  • 举报
回复

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication47
{
class Program
{
static void Main(string[] args)
{
TimeSpan ts = new TimeSpan(100000000);

for (int i = 0; i < 10; i++)
{
Thread.Sleep(1000);
ts= ts.Subtract(new TimeSpan(10000000));
Console.WriteLine(ts.ToString());

}
Console.WriteLine("火箭发射");
Console.ReadKey();
}
}
}

gene31415926 2010-01-17
  • 打赏
  • 举报
回复
晕死了,如果我倒计时是2分钟怎么办啊????
鸭梨山大帝 2010-01-17
  • 打赏
  • 举报
回复

label1.Text ="00 : 00 : " + i.ToString().Length==1?"0"+i.ToString():i.ToString();
i--;
jocklyhu 2010-01-17
  • 打赏
  • 举报
回复
你控制你lable的字符串不就解决了吗?label1.Text ="00 : 00 : " + (i--).ToString();

111,121

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧