Private Function findControlByName( _
ByVal pCurrenControl As Control, _
ByVal pControlName As String _
) As Control
If pCurrenControl.Name.Equals(pControlName) Then
Return pCurrenControl
End If
For Each c As Control In pCurrenControl.Controls
If c.Name.Equals(pControlName) Then
Return c
End If
If c.Controls.Count > 0 Then
Dim result As Control _
= findControlByName(c, pControlName)
If result IsNot Nothing Then
Return result
End If
End If
Next
Return Nothing
End Function
เวลาเอาไปใช้
Code (VB.NET)
Dim c As Control = findControlByName(Me, "PicBox1")
If c Is Nothing Then
MsgBox(" ไม่เจอ PicBox1 ")
Else
c.BackColor = Color.Red
End If