RadioButton.cs

Seungbin Yang / 양승빈·2024년 3월 25일

비주얼프로그래밍

목록 보기
6/21

<디자인>

groupbox1 text를 국적, groupbox2 text를 성별로 바꾼다.
groupbox1 안에 radiobutton을 배치하고 각각 text와 name을 변경한다.

groupbox2 안에도 마찬가지로 radiobutton을 배치한다.

<코드>

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 _012_RadioButton
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string result = "";
            if (rbKorea.Checked)
                result += "국적 : 대한민국\n";
            else if (rbChina.Checked)
                result += "국적 : 중국\n";
            else if (rbJapan.Checked)
                result += "국적 : 일본\n";
            else if (rbOthers.Checked)
                result += "국적 : 그 외 국가\n";

            if (rbMale.Checked)
                result += "성별 : 남성";
            else if (rbFemale.Checked)
                result += "성별 : 여성";

            MessageBox.Show(result, "결과!");
        }
    }
}

result 문자열 변수를 초기화한다.

if, else if 조건문을 통해 국적, 성별을 구분하는 기능을 한다.

MessageBox.Show(result, "결과!");로 국적을 출력한다.

0개의 댓글