C#/WindowForm

C# 체크박스

hyun0229 2022. 3. 30. 02:52

namespace _019_CheckBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string checkStates = "";
            CheckBox[] cBox = { checkBox1, checkBox2, checkBox3, checkBox4, checkBox5 };
            foreach (var item in cBox)
            {
                checkStates += string.Format("{0} : {1}\n", //Format을 사용하여 문자열에 계속 추가
                      item.Text, item.Checked);
            }
            MessageBox.Show(checkStates, "checkStates");
            string summary = string.Format("좋아하는 과일은 : ");
            foreach (var item in cBox)
            {
                if (item.Checked == true)
                    summary += item.Text + " ";
            }
            MessageBox.Show(summary, "summary");


        }
    }
}

 

실행화면
첫번째
두번째