Page 1 of 1

อยาก กำหนด นาที จากโค้ดนี้ครับ แนะนำหน่อยครับ อาจารย์

Posted: Tue Nov 15, 2016 1:24 pm
by Taksaporn

Code: Select all

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.Timers;
using System.Windows.Forms;

namespace TimerTest
{
    public partial class Form1 : Form
    {
        System.Timers.Timer t;
        int h, m, s;

        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            t = new System.Timers.Timer();
            t.Interval = 1000;
            t.Elapsed += OntimeEvent;
           // timer1.Interval = 1000;
        }

        private void OntimeEvent(object sender, ElapsedEventArgs e)
        {
            Invoke(new Action(() =>
          {
              s += 1;
              if (s == 60)
              {
                  s = 0;
                  m += 1;

              }
              if (m == 60)
              {
                  m = 0;
                  h += 1;
              }
              txtResult.Text = string.Format("{0}:{1}:{2}", h.ToString().PadLeft(2, '0'),m.ToString ().PadLeft (2, '0'), s.ToString().PadLeft(2, '0'));
          }));
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            t.Start();
        }
        private void btnstop_Click(object sender, EventArgs e)
        {
            t.Stop();
        }
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            t.Stop();
            Application.DoEvents();
        }
    }
}

Re: อยาก กำหนด นาที จากโค้ดนี้ครับ แนะนำหน่อยครับ อาจารย์

Posted: Wed Nov 16, 2016 9:08 pm
by snasui
:D กรณี Date, Time ใช้ Format หาค่าได้ ลดการเขียน Code เพื่อหา นาที ชม. วัน เองครับ

ดูเพิ่มเติมที่นี่ครับ https://msdn.microsoft.com/en-us/librar ... .110).aspx