C # create a clock program for form programming to display the current time

Source: Internet
Author: User

Simple for beginners
Question: display the current time in the form and simulate the clock
The program runs as follows:

Steps:
1. Open vs2010, File-> new-> project-> WindowsForm application
2. Change the text attribute in the form properties serial port to "Clock"
4. Select to add a label control and a Timer control for the form from toolbox.
5. Enter the code page, add the code to get the current time, and set the text attribute of the label to the display format of the time.
6. Double-click Timer to compile the Tick event of timer. The time display method is as follows:
8. Double-click the form, fill in the load event, and start timer;
9. The Form3.cs code is as follows:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Namespace form1
{
Public partial class Form3: Form
{
Public Form3 ()
{
InitializeComponent ();
}
Public void GetTime ()
{
DateTime time = DateTime. Now; // obtain the current system time
/*
* If the hour, minute, and second attributes of time are directly used to represent the time, the hour (or minute, second) is
* For a single digit, for example, 02:11:06, 02:11:06 is displayed. The displayed format is somewhat different from that of the regular one.
* Format. You need to modify the code;
* As follows, hour is represented by two numbers h1h2. When hour is a single digit, h2 = hour, h1 = 0, and the display is normal;
* If the value of hour is two digits, h1 = hour/10 and h2 = hour % 10 are displayed normally;
* Similarly, minute and second are also displayed.
* The Code is as follows:
**/
Int hour = time. Hour; // get the current hour value
Int h1 = 0;
Int h2 = hour ;;
If (hour> = 10)
{
H1 = hour/10;
H2 = hour % 10;
}
Int minute = time. Minute;
Int m1 = 0;
Int m2 = minute;
If (minute> = 10)
{
M1 = minute/10;
M2 = minute % 10;
}
Int second = time. Second;
Int s1 = 0;
Int s2 = second;
If (second> = 10)
{
S1 = second/10;
S2 = second % 10; www.2cto.com
}
Label1.Text = string. format ("{0} {1 }:{ 2} {3 }:{ 4} {5}", h1, h2, m1, m2, s1, s2 ); // display time of the label Control
}
Private void timereffectick (object sender, EventArgs e)
{
This. GetTime (); // code used to call the Tick event of the time1 control to display the control time
}
Private void Form3_Load (object sender, EventArgs e)
{
This. timer1.Interval = 1000; // set the Interval attribute of timer1 to 1000, that is, the Interval between the timer start time is 1000 ms.
This. timer1.Start (); // start the timer
}
}
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.