With reference to this question
If you put this in a module called ControlExt.vb:
Imports System.Runtime.CompilerServices
Module ControlExt
<Extension()>
Public Function GetAllChildren(Of T As Control)(parentControl As Control) As IEnumerable(Of T)
Dim controls = parentControl.Controls.Cast(Of Control)
Return controls.SelectMany(Of Control)(Function(ctrl) _
GetAllChildren(Of T)(ctrl)) _
.Concat(controls) _
.Where(Function(ctrl) ctrl.GetType() = GetType(T)) _
.Cast(Of T)
End Function
End Module
Then you could do this in your class:
Class WhateverForm
Private _allTextboxes as List(Of TextBox)
Public Sub New() 'in the constructor
InitializeComponent()
_allTextboxes = Me.GetAllChildren(Of TextBox).ToList()
End Sub
Which means all the textboxes on your form, wherever they are (inside whatever subpanels of subpanels, of tabpages of groupboxes etc), are now in a List.. And you can do this in your edit button click handler:
_allTextboxes.ForEach(Sub(b) b.ReadOnly = False)
And of course, perform the corollary ReadOnly = True
in the Save button. You don't have to limit it to textboxes; GetAllChildren can find anything - maybe you want to lock the checkboxes too - have another list of _allCheckboxes
, GetAllChildren(Of CheckBox)
and in the ForEach
set Enabled =
etc
I'd point out that ReadOnly and Enabled are slightly different and as a UX pointer if you're going to make stuff greyed out in a UI (or "half greyed out" in the case of ReadOnly), then you should have something explaining why the option is greyed out/how to enable it. It won't be immediately obvious to users that they have to click Enable and if you don't want tech support calls going "I'm clicking in the name box and I can see the cursor flashing but when i type nothing happens!" then give them a nudge with "View mode: To enable editing of this data, click [EDIT]" etc