ballqs 님의 블로그
[C#] Window Form에서 모든 Panel를 돌면서 RadioButton 건들기 본문
C#에 대해서 작업해야 할 것이 생겼고 Window Form으로 만들어진 부분을
전체 반복문으로 돌면서 RadioButton을 찾고 Check 해야하는 로직이 필요했다.
작업을 진행하게 된 툴은 Microsoft Visual Studio 이며 이 툴 기반으로 그리는 것을 했다.
코드
private void RadioButtonAllCheck(Control parent, string pattern)
{
// control를 전부 돌면서 찾기!
foreach (Control control in parent.Controls)
{
// panel
if (control is Panel panel) // Panel 기준으로 돌기
{
// panel Controls 기준으로 내부 반복문
foreach (Control subControl in panel.Controls)
{
// radiobutton 찾기(pattern에 맞는 이름을 가진 라디오버튼)
if (subControl is RadioButton radioButton && radioButton.Name.Contains(pattern))
{
radioButton.Checked = true; // 패턴이 맞으면 체크
}
}
}
// 하위 컨트롤이 있는 경우 재귀 호출
if (control.HasChildren)
{
RadioButtonAllCheck(control, pattern);
}
}
}
'코딩 공부 > C#' 카테고리의 다른 글
[C#] 문법 정리 - 1 (1) | 2025.02.21 |
---|