Lock Text Boxes (VB 2005)

I have a set of text boxes on a form that I want to be able to lock/unlock at well (ie. user cannot edit text unless permitted).
In VB6 I would simply set the Locked property to True and be done.
In VB 2005, the Locked property is for design time only, and prevents controls from being moved accidentally during form design. Then I found the ReadOnly property and thought it should work.
Here's what I've tried to lock the TextBoxes, but it's not working:

    Private Sub LockFields(ByVal Lock As Boolean)
        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                ctrl.ReadOnly = True
            End If
        Next
    End Sub

It fails with this error in the Error List.
'readonly' is not a member of 'System.Windows.Forms.Control'.
bhlangAsked:
Who is Participating?
 
Carl TawnSystems and Integration DeveloperCommented:
Thats because ctrl is type Control rather than TextBox, so you need to cast it first:

        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).ReadOnly = True
            End If
        Next
0
 
bhlangAuthor Commented:
Thanks. Knew it was something simple, but couldn't figure out what.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.