通过名称找到控件(VB.NET)
1、通过For Each Control in Controls方法查看;
2、通过Reflection查看,代码如下:
Public Function GetControlByName(ByVal Name As String) As Control

'now, why would I put a "_" in front of the name?
Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
System.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.Instance Or _
System.Reflection.BindingFlags.Public Or _
System.Reflection.BindingFlags.IgnoreCase)

If info Is Nothing Then Return Nothing
Dim o As Object = info.GetValue(Me)
Return o

End Function
2、通过Reflection查看,代码如下:
Public Function GetControlByName(ByVal Name As String) As Control
'now, why would I put a "_" in front of the name?
Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, _
System.Reflection.BindingFlags.NonPublic Or _
System.Reflection.BindingFlags.Instance Or _
System.Reflection.BindingFlags.Public Or _
System.Reflection.BindingFlags.IgnoreCase)
If info Is Nothing Then Return Nothing
Dim o As Object = info.GetValue(Me)
Return o
End Function
浙公网安备 33010602011771号