체크 박스 코드로 만들기
체크 박스를 코드로 만드는 과정입니다.
체크 박스를 만들 때, 필요한 텍스트나 가로, 세로 길이 그리고 위치를 지정하여 checkBox1 ~ 3를 만듭니다.
만든 객체들은 반드시 Controls.Add(체크박스 객체) 를 해야 Form에 반영이 됩니다.
코드
public Form1() {
InitializeComponent();
int width = 100;
int height = 23;
int margin = 3;
CheckBox checkBox1 = new CheckBox() {
Text = "사과",
Width = width,
Height = height,
Location = new Point(10, height * 0 + margin)
};
CheckBox checkBox2 = new CheckBox() {
Text = "바나나",
Width = width,
Height = height,
Location = new Point(10, height * 1 + margin)
};
CheckBox checkBox3 = new CheckBox() {
Text = "포도",
Width = width,
Height = height,
Location = new Point(10, height * 2 + margin)
};
Controls.Add(checkBox1);
Controls.Add(checkBox2);
Controls.Add(checkBox3);
Button button = new Button() {
Text = "클릭",
Width = width,
Height = height,
Location = new Point(10, height * 3 + margin)
};
button.Click += ButtonClick;
Controls.Add(button);
}
private void ButtonClick(object sender, EventArgs e) {
List<string> list = new List<string>();
foreach (var item in Controls) {
if (item is CheckBox) {
CheckBox checkBox = (CheckBox)item;
if (checkBox.Checked) {
list.Add(checkBox.Text);
}
}
}
MessageBox.Show(string.Join(", ", list));
}
결과
그룹 박스 코드로 만들기
라디오 박스는 체크 박스와 코드 상으로 사용하는 법은 비슷합니다.
하지만 두 객체의 성격은 다릅니다. 체크 박스는 다중, 라디오 박스는 단일이라는 특징이 있습니다.
GroupBox를 선언하여 각 카테고리 별로 라디오 박스를 만들려고 합니다.
카테고리는 과일, 채소, 동물, 등 ... 과 같이 상황에 맞게 분류할 수 있죠.
코드 상에서 주의 해야할 부분은 각 카테고리에 따라서 (그룹 박스 객체).Controls.Add(라디오 박스 객체)를 선언 해야합니다.
이후, 카테고리 별로 선언된 GroupBox 객체를 From에 넘기기 위한 Controls.Add(그룹 박스 객체) 을 합니다.
코드
public Form1() {
InitializeComponent();
int width = 100;
int height = 23;
int margin = 3;
GroupBox groupBox1 = new GroupBox() {
Text = "과일",
Location = new Point(margin, margin),
Size = new Size(width + margin * 2, height * 3 + margin * 2)
};
GroupBox groupBox2 = new GroupBox() {
Text = "채소",
Location = new Point(margin * 5 + width, margin),
Size = new Size(width + margin * 2, height * 3 + margin * 2)
};
RadioButton radioButton1 = new RadioButton() {
Text = "사과",
Width = width,
Height = height,
Location = new Point(10, height * 1 + margin)
};
RadioButton radioButton2 = new RadioButton() {
Text = "바나나",
Width = width,
Height = height,
Location = new Point(10, height * 2 + margin)
};
RadioButton radioButton3 = new RadioButton() {
Text = "시금치",
Width = width,
Height = height,
Location = new Point(10, height * 1 + margin)
};
RadioButton radioButton4 = new RadioButton() {
Text = "당근",
Width = width,
Height = height,
Location = new Point(10, height * 2 + margin)
};
// 주의!
groupBox1.Controls.Add(radioButton1);
groupBox1.Controls.Add(radioButton2);
groupBox2.Controls.Add(radioButton3);
groupBox2.Controls.Add(radioButton4);
Controls.Add(groupBox1);
Controls.Add(groupBox2);
Button button = new Button() {
Text = "클릭",
Width = width,
Height = height,
Location = new Point(margin, height * 3 + margin * 4)
};
button.Click += ButtonClick;
Controls.Add(button);
}
private void ButtonClick(object sender, EventArgs e) {
List<string> list = new List<string>();
foreach (var item in Controls) {
if (item is GroupBox) {
foreach (var innerItem in ((GroupBox)item).Controls) {
RadioButton radioButton = innerItem as RadioButton;
if (radioButton != null && radioButton.Checked) {
MessageBox.Show(radioButton.Text);
}
}
}
}
}
결과
'개발 기록 > C#' 카테고리의 다른 글
[C#] 윈도우 폼 - 콤보 박스, 리스트 박스 사용하기 (0) | 2021.12.09 |
---|---|
[C#] Dictionary<T, T> 사용하기 (0) | 2021.12.09 |
[C#] delegate vs 무명 delegate, lamba 란? (0) | 2021.11.22 |
[C# 기본기] out 키워드란? (0) | 2021.10.25 |
댓글