[C#]006_My006App_電子錶

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;  //顯示時間
}

 

顯示效果如下:

 

image

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章