C#/VB.NET Panel 안의 Control 반복하여 원하는 Control 찾기

2020. 1. 14. 14:10C#

728x90
반응형

 

 

 

C#

/// <summary>
/// 지정 된 nName 이라는 버튼 이름을 가지고
/// 패널위치를 찾고 버튼을 눌러준다.
///</summary>
///<param name="mName"></param>
private void ShowPanelScreen(ref string mName)
{
    if (mName != "")
    {
        // 메인 패널들
        foreach (Control p in panels)
        {
            // 메인 패널 안의 패널 
            foreach (Control ps in p.Controls)
            {
                if ((ps.GetType == typeof(Button)))
                {
                    System.Diagnostics.Debug.Print("Name : " + ps.Name + ", type : " + ps.GetType.ToString);

                    if ((mName == ps.Name))
                    {
                        Button btn = ps;
                        btn.PerformClick();
                    }
                }
            }
        }
    }
}






VB.NET

    ''' <summary>
    '''  지정 된 nName 이라는 버튼 이름을 가지고
    '''  패널위치를 찾고 버튼을 눌러준다.
    ''' </summary>
    ''' <param name="mName"></param>
    Private Sub ShowPanelScreen(ByRef mName As String)

        If mName <> "" Then
            ' 메인 패널들
            For Each p As Control In panels
                ' 메인 패널 안의 패널 
                For Each ps As Control In p.Controls
                    If (ps.GetType = GetType(Button)) Then
                        Diagnostics.Debug.Print("Name : " & ps.Name & ", type : " & ps.GetType.ToString)

                        If (mName = ps.Name) Then
                            Dim btn As Button = ps
                            btn.PerformClick()

                        End If

                    End If
                Next
            Next

        End If

    End Sub


Panel 안 Panel 들을 반복하여 버튼 Control을 찾는 코드 임

Control 안에 Control 안에 특정 Control을 찾을 수 있음

Name : btn_member, type : System.Windows.Forms.Button 
Name : btn_manage, type : System.Windows.Forms.Button 
Name : btn_AddModel, type : System.Windows.Forms.Button 
Name : btnAssignMember, type : System.Windows.Forms.Button 
Name : btnFrameAdd, type : System.Windows.Forms.Button 
Name : btn_add_defect, type : System.Windows.Forms.Button 
Name : btn_write_random, type : System.Windows.Forms.Button 
Name : btn_write_tc, type : System.Windows.Forms.Button 
Name : btn_random_view, type : System.Windows.Forms.Button 
Name : btn_share, type : System.Windows.Forms.Button 
Name : btn_search, type : System.Windows.Forms.Button

Button을 찾을 때마다 control Name과 control type을 찍어보면서 원하는 control을 찾을 수 있다.



 

728x90
반응형