[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

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'.
0
bhlang
Asked:
bhlang
1 Solution
 
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now