Oct 12, 2015

c# Basic] radio button






I changed layout.


Left is new layout.

I added radio button.

radio button '타이머 모드' is Timer mode.

radio button '카운터 모드'is Counter mode.

Also I added new variable 'timermode'.

1
Boolean timermode = true;

true is '타이머 모드(Timer mode)', false is '카운터 모드(Counter mode)'.


below code is radio button code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (timerCount == 0)
            {
                timermode = true;
                timer_init();
            }
       }
 
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if(timerCount==0)
            {
                timermode = false;
                timer_init();
            }
       }

Line 1~13 is '타이어 모드'(Timer mode), 15~27 is '카운터 모드'(counter mode)


Both '타이머 모드' and '카운터 모드' working as 'timerCout' is '0'.

'timer_init()' is changing setting.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
private void timer_init()
        {
            timerCount = 0;
            if (timermode == true)
            {
                timerText.Text = "00:00:00";
                label2.Text = "타이머 테스트";
            }
            else
            {
                timerText.Text = "0";
                label2.Text = "카운터 테스트";
            }
        }


'timer_display()' is for display information.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
private void timer_display(object sender, EventArgs e)
        {
            timerCount++;
            if(timermode==true)
            {
                int sec = 0, min = 0, hour = 0;
                if (timerCount < 60)
                {
                    sec = timerCount;
                }
                else if (timerCount < 3600)
                {
                    min = timerCount / 60;
                    sec = timerCount % 60;
                }
                else
                {
                    hour = timerCount / 3600;
                    min = (timerCount % 3600) / 60;
                    sec = ((timerCount % 3600) % 60);
                }
               timerText.Text = hour.ToString("00") + ":" + min.ToString("00") + ":" + sec.ToString("00");
            }
            else
            {
                timerText.Text = timerCount.ToString();
            }
          }


Line 4~28 : for timer mode.

Line 29~33 : for counter mode.

Also, radio button deactivation as working timer or counter.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
private void button1_Click(object sender, EventArgs e)
{
      timer1.Start();
      textBox1.Text = "테스트를 시작합니다.";
      radioButton1.Enabled = false;
      radioButton2.Enabled = false;
}
 
private void button2_Click(object sender, EventArgs e)
{
      timer1.Stop();
      textBox1.Text = "테스트를 종료합니다.";
      radioButton1.Enabled = true;
      radioButton2.Enabled = true;
      timer_init();
}

Line 5~6 : Deactivation Radio button when working timer.

Line 13~14 : Activation Radio button when not working timer.

Run~


This is main.


Start timer mode.

Radio button is deactivation.



press '종료'(end) button.

Radio button is activation.




Start counter mode.

Tip!!

Radio button need Group!!

If not include group, radio button not work.

No comments:

Post a Comment