计时问题 例如: 窗体运行时开始计时,点击某按钮输出运行时间(从FormLoad 到 ButtonClick 间的时间) 如何实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Windows.Forms; class Demo : Form{ Demo() { DateTime time0 = DateTime.Now; Button btn = new Button(); btn.Parent = this; btn.Text = "Click me"; btn.Click += delegate { MessageBox.Show((DateTime.Now - time0).ToString()); }; } [STAThread] static void Main() { Application.Run(new Demo()); }} 可以使用如下的类来实现 :StopwatchSystem.Diagnostics.Stopwatch 在Form类里面定义两个全局变量一个是bool first = true,一个是datetime t1, t2formload的第一行代码是t1 = DateTime.nowbuttonClick下面写if (first){ t2 = DateTime.now timespan = t2 - t1;//ts就是时间差了 first = false;} Form_Load 里 : m_start = Environment.TickCountButton_Click 里: m_now = Environment.TickCount运行时间 = m_now - m_start (单位为 milliseconds)然后自己转化为 时分秒的格式就行了 看大家的了! 帮忙看看连数据库时候为啥出System.InvalidOperationException String与string的区别是什么? wince下的listview控件滚动条问题 最后的分数了,请大侠帮忙,谢谢 两个数据库之间数据转换的问题 richTextBox问题 问个学习问题 连接数据库失败为什么? 为什么我的Visual Studio.net无法安装。是关于组件的问题 请教个DevExpress.GridControl导出EXCEL的问题. C#中怎么用ComboBox实现style,谢谢。
using System.Windows.Forms; class Demo : Form
{
Demo()
{
DateTime time0 = DateTime.Now;
Button btn = new Button();
btn.Parent = this;
btn.Text = "Click me";
btn.Click += delegate { MessageBox.Show((DateTime.Now - time0).ToString()); };
}
[STAThread]
static void Main()
{
Application.Run(new Demo());
}
}
StopwatchSystem.Diagnostics.Stopwatch
一个是bool first = true,一个是datetime t1, t2formload的第一行代码是t1 = DateTime.nowbuttonClick下面写if (first)
{
t2 = DateTime.now
timespan = t2 - t1;//ts就是时间差了
first = false;
}
Button_Click 里: m_now = Environment.TickCount运行时间 = m_now - m_start (单位为 milliseconds)然后自己转化为 时分秒的格式就行了