C#

C# 간단한 타이머 예제 따라하기

Mons 2007. 11. 26. 22:08

프로그램 외형

프로그램 외형은 위와 같습니다.
아직 60분이 넘으면 0분으로 돌아가게 끔은 하지 않았습니다...(몰라서?)
일단 바깥에 60밀리초, 60초 가 되면 TextBox에 숫자를 증가시켜 주기 위해 계속 증가 시켜갈 변수 두개를 선언했습니다!
시작은 button1 종료는 button2 분은 txtMin 초는 txtSec 밀리초는 txtMil로 했습니다.
보시다시피 시작 누르면 타이머 작동하고 종료 누르면 잠시 멈춥니다.
그리고 다른 소스는..(..) 알아서 잘 이해를...
전 아직 네임스페이스가 뭔지도 모르고 OOP도 뭔지 모르는 초보니까요~_~ 같이 배워 갑시다!


namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        int mil=0,sec=0;

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            timer1.Start();

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            timer1.Enabled = false;

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            txtMil.Text = Convert.ToString(Convert.ToInt32(txtMil.Text) + 1);

            mil = Convert.ToInt32(txtMil.Text);

 

            if (mil == 60)

            {

                txtSec.Text = Convert.ToString(Convert.ToInt32(txtSec.Text) + 1);

                mil = 0;

                txtMil.Text = "0";

            }

        }

 

        private void txtSec_TextChanged(object sender, EventArgs e)

        {

            sec = Convert.ToInt32(txtSec.Text);

 

            if (sec == 60)

            {

                txtMin.Text = Convert.ToString(Convert.ToInt32(txtMin.Text) + 1);

                sec = 0;

                txtSec.Text = "0";

            }

        }

    }

}