Results 1 to 1 of 1

Thread: How to browse all controls/containers of a windows form using reflection

  1. #1

    Thread Starter
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    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:
    1. Imports System.Reflection
    2.  
    3.  
    4. Public Sub DisableAllControls(ByVal f As Form)
    5.  
    6.         Dim myForm As Type = f.GetType()
    7.         Dim fields As FieldInfo() = myForm.GetFields(BindingFlags.Instance Or BindingFlags.NonPublic)
    8.         For Each field As FieldInfo In fields
    9.             Console.WriteLine(field.FieldType.Name)
    10.             If field.FieldType.Name.ToLower = "textbox" Then
    11.                 Dim t As TextBox = DirectCast(field.GetValue(f), TextBox)
    12.                 t.Enabled = False
    13.             End If
    14.             If field.FieldType.Name.ToLower = "combobox" Then
    15.                 Dim t As ComboBox = DirectCast(field.GetValue(f), ComboBox)
    16.                 t.Enabled = False
    17.             End If
    18.             If field.FieldType.Name.ToLower = "button" Then
    19.                 Dim t As Button = DirectCast(field.GetValue(f), Button)
    20.                 t.Enabled = False
    21.             End If
    22.  
    23.             If field.FieldType.Name.ToLower = "listbox" Then
    24.                 Dim t As ListBox = DirectCast(field.GetValue(f), ListBox)
    25.                 t.Enabled = False
    26.             End If
    27.  
    28.         Next
    29.     End Sub
    Last edited by Asgorath; Oct 27th, 2004 at 12:52 PM.
    "The dark side clouds everything. Impossible to see the future is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width