Tuesday 12 November 2013

c# timer

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Timers;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string GetTime()
        {
            string time = "";
            int hour = DateTime.Now.Hour;
            int min = DateTime.Now.Minute;
            int sec = DateTime.Now.Second;

            time = (hour < 10) ? "0" + hour.ToString() : hour.ToString();
            time += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString());
            time += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString());

            return time;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (sender == timer1)
            { label1.Text = GetTime(); }
        }


    }
}

No comments:

Post a Comment