C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » .NET »

TimeSetting


Posted Date: 07 Nov 2009      Posted By: Naresh      Member Level: Silver     Points: 1   Responses: 2



Hi All,
c# windows application
My Application is online Exam Application
i have to display time to complete the exam .i will use one label if suppose time for the exam is 15 mins it should start from 15mins and time should get decrement by seconds user should be able see the time remaining for the exam .when it reach zero my application should stop





Responses

Author: Pavan    07 Nov 2009Member Level: BronzeRating: 3 out of 53 out of 53 out of 5     Points: 3

Try This it may help you......



using System;
using System.Windows.Forms;
using System.Drawing;

class TimerDemo : Form
{
Timer Clock;
Label lbTime = new Label();
private int hour=0, min = 2, sec = 30;
public TimerDemo()
{
this.Controls.Add(lbTime);

Clock = new Timer();
Clock.Interval = 1000;
Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
lbTime.BackColor = Color.Black;
lbTime.ForeColor = Color.Red;
lbTime.Font = new Font("Times New Roman", 15);
lbTime.Text = GetTime();
}

public string GetTime()
{
string TimeInString = "";
Boolean finished = false;
sec--;
if (sec < 0)
{
min--;
if (min < 0)
{
if (hour > 0)
{
hour--;
min = 59;
sec = 59;
}
else
{
finished = true;
}
}
else
{
sec=59;
}
}
if (finished)
{
TimeInString = "Time's UP";

Clock.Stop();
}
else
{
TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString();
TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString());
}
return TimeInString;
}

public void Timer_Tick(object sender, EventArgs eArgs)
{
if (sender == Clock)
{
lbTime.Text = GetTime();
}
}

public static void Main()
{
Application.Run(new TimerDemo());
}
}



Author: Thillai NathaN    09 Nov 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 2

hi Naresh,

Based on the trick event of the timer the following process goes. The following code shows the remaining time in seconds. you can change it by doing math calculations so as to display the remaining time in minutes and seconds.

int i = 15000 // 1000ms = 1 second.
public void time()
{
time1.Interval = 15000;
time1.Enabled = true;
time1.Start();
time1.Tick += new EventHandler(time1_Tick_1);

}
private void time1_Tick_1(object sender, EventArgs e)
{
if (i >0)
{
lbltime.Text = i.ToString();
i--;
}
}



Post Reply
You must Sign In to post a response.
Next : OOPs
Previous : Button Click
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use