Search code examples
vb.netvisual-studiocontrolseditreadonly

quick way to disable all textboxfields unlock with edit button


Does Vb.net have a quick way to disable all editable fields (textboxes, checkboxes,...) in a form? I'm trying to protect data from "accidentally" being overwritten. My user has to click an edit button to "unlock" the fields and make them editable and the user must press the save button to update the database and lock the fields once more.

So far i've only found a manual input to make every textbox and checkbox (about 30 of them in different tabs of the form) readonly values to true and disable them if i click on my edit button. Any advice on how to do this? can i do a "for each" in a quicker way? how would i formulate the code? i found a 2007 article with this snippet:

For Each control As Control In Me.Controls

If TypeOf (control) Is TextBox Then

CType(control, TextBox).ReadOnly = True

End If
Next

but i'd need all types of field in one go.

thanks for any advice on this subject. I'm a beginner.


Solution

  • Try this:

    For Each control As Control In Me.Controls
      Try 
        CType(control, TypeOf(control)).ReadOnly = True
      Catch 
        ' Do nothing
      End Try
    Next