Hello,
I am a complete beginner and am building my first app.
I am trying to show the actual time in my form, and so far I can display it ok using the DateTime structure and it's methods. The problem I have is: I don't know how to make it update automatically.
Any help will be greatly appreciated.
Thanks,
Gabriel
Tom Diann16 pointsPosted Jan 16, 2006, 9:57 PM EST
then you can put your code of displaying current time in its Tick Event.
That's ok!
Jia En30 pointsPosted Jan 17, 2006, 1:02 AM EST
public string GetTime()
{
string TimeInString = "";
// Get current time
int hour = DateTime.Now.Hour;
int min = DateTime.Now.Minute;
int sec = DateTime.Now.Second;
// Format current time into string
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;
}
private void timer1_Tick(object sender, System.EventArgs e)
{
if( sender == timer1 )
{
label1.Text = GetTime();
Invalidate();
}
}
put this code in.. and set ur Timer interval as 1000.. Hope its help..
Gabriel Gonzalez3 pointsPosted Jan 17, 2006, 8:04 AM EST
Thank you very much for your replies. I will try them later today but I am sure this is what I am looking for.
This site is great, I really appreciate your help.
Forgive me if I ask any more 'dumb' questions. But self learning C is not as easy as it would seem. There is an overload of information on the web and it is easy to get lost and confused.
When searching for info I always get thousands of hits, really hard to sort thru.
Thanks again,
Gabriel
AnttiRank #9,295 · 208 pointsPosted Jan 18, 2006, 12:12 AM EST
Here's much more elegant way to format datetime to string:
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { DateTime now = DateTime.Now; //gets the short time format string shortTime = now.ToShortTimeString(); //gets your special time format, more info //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp string specialTime = now.ToString("hh:mm:ss"); //gets the long datetime format corresponding to your locality settings string localizedDateTime = now.ToString( System.Threading.Thread.CurrentThread.CurrentCulture); Console.WriteLine(shortTime); Console.WriteLine(specialTime); Console.WriteLine(localizedDateTime); } } }Much more formatting options awailable in DateTime class...