如何在charp中为Quiz应用程序计算Reamaining时间(以分钟为单位) [英] How to calulate the Reamaining time in minutes for Quiz application in charp
问题描述
您好我正在开发一些Quiz应用程序,我想知道如何在用户点击开始测验按钮时向用户显示剩余时间为30分钟。在尖锐的Windows应用程序中.plz help
Hello I am Developing some Quiz application for that i want to know how to show the remaining time for user as 30 Minutes to the user when he clicks on the start quiz button .in c sharp windows application .plz help
推荐答案
那么?设置一个计时器和一个剩余时间变量:
So? Set up a Timer, and a time left variable:
private Timer time = new Timer();
private int timeRemaining;
在Form构造函数中添加所需的interval和事件处理程序:
In your Form constructor add the required interval, and the event handler:
time.Interval = 500;
time.Tick += new EventHandler(time_Tick);
timeRemaining = 30 * 60 * 2;
time.Start();
timeRemaining值为30分钟,以秒为单位,以每秒滴答为单位(间隔为500,每半秒)
然后Tick事件处理程序:
The timeRemaining value is 30 minutes, in seconds, in ticks per second (interval of 500 is every half second)
Then the Tick event handler:
void time_Tick(object sender, EventArgs e)
{
if (timeRemaining > 0)
{
timeRemaining--;
int seconds = timeRemaining / 2;
int minutes = seconds / 60;
labTimeRemaining.Text = string.Format("{0:00}:{1:00}", minutes, seconds % 60);
}
}
有什么问题?
初始化DatetTime
对象(比如完成
),
What is the problem?
Initialize aDatetTime
object (sayfinish
) with
finish = DateTime.Now + TimeSpan.FromMinutes(30);
然后,稍后(只要你需要),剩余的分钟数将是
then, later (whenever you need it), the remaining minutes would be
int remaining = 0;
DateTime now = DateTime.Now;
if ( finish > now)
remaining = (int) (finish - now).TotalMinutes;
private void Form1_Load(object sender, EventArgs e)
{
Watch w = new Watch(new TimeSpan(0, 30, 0)); // assign the time from where clock start.
var rTime1 = w.RemainingTime; // further more you can use timer and in timer's elapsed event continuousely get w.RemaningTime
Thread.Sleep(5000);
rTime1 = w.RemainingTime;
Thread.Sleep(10000);
rTime1 = w.RemainingTime;
}
public class Watch
{
private DateTime _time;
public Watch(TimeSpan time)
{
_time = DateTime.Now.Add(time);
}
public TimeSpan RemainingTime
{
get
{
var remainingTime = _time - DateTime.Now;
if (remainingTime < TimeSpan.Zero)
{
return TimeSpan.Zero;
}
else
{
return remainingTime;
}
}
}
}
这篇关于如何在charp中为Quiz应用程序计算Reamaining时间(以分钟为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!