006_My006App_電子錶
先添加一個時鐘控件, Enable屬性設置爲Trun; Interval屬性設置爲1000
Code:
private void timer1_Tick(object sender, EventArgs e) //時鐘控件
{
string timNewTime = "";
string timNewHour = Convert.ToString(DateTime.Now.Hour);
//定義“時”的值
if (Convert.ToInt32(timNewHour)<10) //值小於10的時候,在前面加0
{
timNewHour = "0" + timNewHour;
}
string timNewMinute = Convert.ToString(DateTime.Now.Minute);
if (Convert.ToInt32(timNewMinute) < 10) //值小於10的時候,在前面加0
{
timNewMinute = "0" + timNewMinute;
}
string timNewSecond = Convert.ToString(DateTime.Now.Second);
if (Convert.ToInt32(timNewSecond) < 10) //值小於10的時候,在前面加0
{
timNewSecond = "0" + timNewSecond;
}
timNewTime = timNewHour + ":" + timNewMinute + ":" + timNewSecond;
label1.Text = timNewTime; //顯示時間
}
顯示效果如下: