Thursday, July 9, 2009

I 've created a c# "clock class" and i am trying to make it continously update in my windows form, see details

I created my clock class in C# and am now trying to have it display in a windows form and continously update. Here is the class code:





class JClock


{


Timer Clock;


public JClock()


{


Clock = new Timer();


Clock.Interval = 1000;


Clock.Start();


Clock.Tick += new EventHandler(Timer_Tick);


}


public string getTime()


{


string TimeInString = "";


int hour = DateTime.Now.Hour;


int min = DateTime.Now.Minute;


int sec = DateTime.Now.Second;





TimeInString = (hour %26lt; 10) ? "0" + hour.ToString() : hour.ToString();


TimeInString += ":" + ((min %26lt; 10) ? "0" + min.ToString() : min.ToString());


TimeInString += ":" + ((sec %26lt; 10) ? "0" + sec.ToString() : sec.ToString());


return TimeInString;


}

I 've created a c# "clock class" and i am trying to make it continously update in my windows form, see details
In the constructor for JClock I would pass in a reference to the label control. Then in the Timer_Tick event I would set the label text to the result of getTime(). All UI updating has to occur on the main thread, so if you get an error while trying to do this, you might have to use the Invoke() method on the label to marshal the call to the thread with the message loop.
Reply:It looks ok, but u need to have Handler


public void Timer_Tick(object sender,EventArgs eArgs)


{


if(sender==Clock)


{


lbTime.Text = GetTime();


}


}





and also the


using System;


using System.Windows.Forms;


using System.Drawing;

pear

No comments:

Post a Comment