Skip to content

Instantly share code, notes, and snippets.

@wolf99
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolf99/a4eabf4e0755be093369 to your computer and use it in GitHub Desktop.
Save wolf99/a4eabf4e0755be093369 to your computer and use it in GitHub Desktop.
Extensions to change the Enabled state of a given type of VB.NET control in collection of controls, recursing through any child controls
'Usage: Form1.Controls.Enabled(Of Button)(False)
Imports System.Runtime.CompilerServices
Module Extensions
<Extension()>
Public Sub Enabled(Of T As Control)(cc As Control.ControlCollection, state As Boolean)
For Each c As Control In cc ' Iterate through every object in the container
If TypeOf c Is T Then ' Check if the object matches the type to set the state on
CType(c, T).Enabled = state ' Set the state on the matching object
ElseIf c.HasChildren Then ' Check if control is a parent ("Congratulations, omg look at that cute semi-colon!")
Enabled(Of T)(c.Controls, state) ' Recurse to handle the children (else maybe get a nanny)
End If
Next
End Sub
<Extension()>
Public Sub Enabled(Of T As Control)(c As Control, state As Boolean)
For Each ci As Control In c.Controls
If TypeOf ci Is T Then
CType(ci, T).Enabled = state
ElseIf ci.HasChildren Then
Enabled(Of T)(ci, state)
End If
Next
End Sub
<Extension()>
Public Sub Enabled(Of T As ToolStripMenuItem)(c As ToolStripItemCollection, state As Boolean)
For Each ci As ToolStripItem In c
If TypeOf ci Is T Then
ci.Enabled = state
End If
Next
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment