How to browse all controls/containers of a windows form using reflection
Hi
In this example i am using System.Reflection to disable several types of controls.
VB Code:
Imports System.Reflection
Public Sub DisableAllControls(ByVal f As Form)
Dim myForm As Type = f.GetType()
Dim fields As FieldInfo() = myForm.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
For Each field As FieldInfo In fields
Console.WriteLine(field.FieldType.Name)
If field.FieldType.Name.ToLower = "textbox" Then
Dim t As TextBox = DirectCast(field.GetValue(f), TextBox)
t.Enabled = False
End If
If field.FieldType.Name.ToLower = "combobox" Then
Dim t As ComboBox = DirectCast(field.GetValue(f), ComboBox)
t.Enabled = False
End If
If field.FieldType.Name.ToLower = "button" Then
Dim t As Button = DirectCast(field.GetValue(f), Button)
t.Enabled = False
End If
If field.FieldType.Name.ToLower = "listbox" Then
Dim t As ListBox = DirectCast(field.GetValue(f), ListBox)
t.Enabled = False
End If
Next
End Sub