November 20, 2011

Simple Count Up Timer

วิธีการสร้างนาฬิกาจับเวลาและตัวนับถอยหลังแบบง่ายๆ
นาฬิกาจับเวลา
ตัวอย่างโค้ดของโปรแกรม :
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 Timer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            second.Text = (int.Parse(second.Text) + 1).ToString();
            if (int.Parse(second.Text) < 10) second.Text = "0" + second.Text;
            if (int.Parse(second.Text) == 60)
            {
                min.Text = (int.Parse(min.Text) + 1).ToString();
                second.Text = "00";
                
                if (int.Parse(min.Text) < 10) min.Text = "0" + min.Text;
                if (int.Parse(min.Text) == 60)
                {
                    hour.Text = (int.Parse(hour.Text) + 1).ToString();
                    min.Text = "00";
                    if (int.Parse(hour.Text) < 10) hour.Text = "0" + hour.Text;
                }
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer.Enabled = true;
            timer.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer.Stop();
            timer.Enabled = false;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            timer.Stop();
            timer.Enabled = false;
            hour.Text = "00";
            min.Text = "00";
            second.Text = "00";
        }
    }
}

No comments:

Post a Comment