コントロールコレクションから指定したコントロールのみ取得したい時、通常FindControlを使用するが、型指定で取得したい時は下記のようにしています。
VB.NETで記述してあります。
001 '[WebForms] 002 Public Shared Function GetControl(ByVal Parent As Control, ByVal Type As System.Type, Optional ByVal Name As String = "") As Control 003 Dim objControl As Control 004 005 If Name.Length() > 0 Then 006 Return Parent.FindControl(Name) 007 End If 008 009 For Each objControl In Parent.Controls 010 If objControl.GetType() Is Type Then 011 Return objControl 012 End If 013 If objControl.Controls.Count > 0 Then 014 Dim tmpControl As Control 015 tmpControl = GetControl(objControl, Type, Name) 016 If Not tmpControl Is Nothing Then 017 Return tmpControl 018 End If 019 End If 020 Next 021 Return Nothing 022 End Function 023 024 '[WindowsForms] 025 026 Public Shared Function GetControl(ByVal Parent As Control, ByVal Type As System.Type, Optional ByVal Name As String = "") As Control 027 Dim objControl As Control 028 029 030 For Each objControl In Parent.Controls 031 If Name.Trim.Length = 0 OrElse Name = objControl.Name Then 032 If objControl.GetType() Is Type Then 033 Return objControl 034 End If 035 End If 036 037 If objControl.Controls.Count > 0 Then 038 Dim tmpControl As Control 039 tmpControl = GetControl(objControl, Type, Name) 040 If Not tmpControl Is Nothing Then 041 Return tmpControl 042 End If 043 End If 044 Next 045 Return Nothing 046 End Function 047