2013. 1. 9. 15:13

C# Timer 를 이용한 Progressbar

C# Timer 를 이용하여 매 1초마다 Progressbar 1 Setp 가 이동하게 한상태입니다.







## 소스


public Form1()

        {
            InitializeComponent();
           
            // 1초 간격 tick 발생
            timer_progress.Interval = 1000;  
            // 초기 이것땜에 값이 2씩 증가하는 현상 발생. ㅠㅠ
            //timer_progress.Tick += new EventHandler(timer_progress_Tick);
 
        }


  private void Form1_Load(object sender, EventArgs e)
        {
           //progressbar 스타일 지정
            progressBar1.Style = ProgressBarStyle.Blocks;

            //최소값 지정(기본값)
            progressBar1.Minimum = 0;

            //최대값 지정(기본값 =10)
            progressBar1.Maximum = 240;
 
            // 1씩 증가
            progressBar1.Step = 1;

            progressBar1.Value = 0;
            
 
        }
 


  // 타임어 설정

   private void timer_progress_Tick(object sender, EventArgs e)
        {
 
            // 숫자표시
            lab_progress.Text = progressBar1.Value.ToString() + "초";
            label2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
 
            //Step 에서 지정된 값으로 진행
            progressBar1.PerformStep();
 
            // 타이머 중지 조건
            if (progressBar1.Value == 240)
            {
                progressBar1.Value = 0;
                lab_progress.Text = "0";
                timer_progress.Enabled = true;
            }         
            
        }