Welcome to My World (www.dgmayor.com)

소프트웨어/자바 GUI & C# 등...

52. C# Winform Timer 예제....

dgmayor 2022. 8. 1. 21:31
728x90

1. design class

2. main_function class

3. function class(timer)

 

ex :

1. timer

using System;
using System.Timers;

namespace Timer1
{
    internal class B_Timer
    {

        Timer tm2;
        int ss = 0; int mm = 0; int hh = 0;
        string var = "- - - - - -";

        public void Init(int interval)
        {
            tm2 = new Timer();
            tm2.Interval = interval;
            tm2.Elapsed += OnTimedEvent;
        }


        private void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            ss++;

            if(ss == 60)
            {
                ss = 0;
                mm++;

                if(mm == 60)
                {
                    mm = 0;
                    hh++;
                    {
                        if(hh == 24)
                        {
                            ss = 0; mm = 0; hh = 0;
                        }
                    }
                }
            }

            var = hh + "시 " + mm + "분 " + ss + "초";

            
        }

        public void Start()
        {
            tm2.Start();
        }

        public void Stop()
        {
            tm2.Stop();
            
        }

        public void Reset()
        {
            ss = 0; mm = 0; hh = 0;
            tm2.Stop();
            //tm2.Dispose();
        }

        public String result()
        {
            return var;
        }

    }



}

 

2. function

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;

namespace Timer1
{
    public partial class Form1 : Form
    {
        B_Timer bt = new B_Timer();

        public Form1()
        {
            bt.Init(1000);
            InitializeComponent();
        }

        private void timer_btn_Click(object sender, EventArgs e)
        {


            if (timer_btn.Text == "S T A R T")
            {
                timer_btn.Text = "S T O P";
                A_timer.Start();
                bt.Start();

            } else
            {
                timer_btn.Text = "S T A R T";
                A_timer.Stop();
                bt.Reset();

            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer_la.Text = bt.result();
        }

    }
}

 

인정하기 싫은데;;; C# 생각 보다 좋다....

근데, 빌게이츠가 싫다.

그래서 나중에 기회 되면 다시 자바로 넘어가야겠다.

 

그건 그렇고.... 대학교 4학년 동안 구구단 배운 거 말고 뭘 배웠는지 기억이 나질 않는다;;

나도 전자과나 갔어야 하는데;;

728x90