We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Lock Text Boxes (VB 2005)

bhlang asked
Medium Priority
Last Modified: 2008-01-09
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
    End Sub

It fails with this error in the Error List.
'readonly' is not a member of 'System.Windows.Forms.Control'.
Watch Question

Senior Systems and Integration Developer
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Thanks. Knew it was something simple, but couldn't figure out what.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.