IT/C#

C# - 특정 Control 찾아서 초기화

!? 2023. 8. 18.

MSDN 참조
public Syste m.Windows.Forms.Control[] Find (string key, bool searchAllChildren);

key - 찾을 control name
searchAllChildren - 모든 자식 컨트롤을 검색 true, 그렇지 않으면 false

group box나 tablelayout 같은 control들은 재귀함수로.

아래는 Sample 코드입니다.

private void BtnSearch_Click(object sender, EventArgs e)
{
	try
	{                
		ControlClear(otlInput.Name);               
	}
	catch (Exception ex)
	{
	}
}


private void ControlClear(string ctlName)
{
    System.Windows.Forms.Control[] ctl = this.Controls.Find(ctlName, true);
    foreach (System.Windows.Forms.Control x in ctl[0].Controls)
    {                
        if (x is TextBox)
        {
            x.Text = "";
        }
        else if (x is NumberEdit)
        {
            x.Text = "0";
        }
        else if (x is MemoBox)
        {
            ((MemoBox)x).Text = "";
        }
        else if (x is DateEdit)
        {
            ((DateEdit)x).Text = "";
        }
        else if (x is TableLayout)
        {
        	// TableLayout은 재귀함수 호출
            ControlClear(x.Name);
        }
    }
}

'IT > C#' 카테고리의 다른 글

C# - ClickOnce 버전 보이기  (0) 2022.02.18
C# - Dapper로 MS-SQL DB 처리하기  (0) 2022.02.14
C# - [winform] MDI프로젝트에서 로그인폼 만들기  (0) 2022.02.10
C# - log4net 적용하기  (0) 2022.02.08
C# - [Winform] 중복실행 방지  (0) 2022.02.07

댓글