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

x

Lock Text Boxes (VB 2005)

bhlang
bhlang asked
on
Medium Priority
327 Views
Last Modified: 2010-04-23
I have a set of Text Boxes on a form that I am trying to lock. They're in various containers on that form however, so I have to do multiple loops to get them all. Is there an easier way to do this?

Here's my code (Thanks to carl_tawn for showing me the CType method):

    Private Sub LockFields(ByVal Lock As Boolean)
        For Each ctrl As Control In Me.SplitContainer2.Panel2.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).ReadOnly = Lock
            End If
        Next
        For Each ctrl As Control In Me.TabPage1.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).ReadOnly = Lock
            End If
        Next
        For Each ctrl As Control In Me.TabPage2.Controls
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).ReadOnly = Lock
            End If
        Next
    End Sub

Comment
Watch Question

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Do you want all TextBoxes on your form to be locked?  Or just the ones in those containers?

Author

Commented:
I want to lock all the text boxes on the form in this case.
Commented:
   Private Sub LockFields(ByVal thisControl As Control, ByVal Lock As Boolean)
        For Each ctrl As Control thisControl
            If ctrl.HasChildren Then
                LockFields(ctrl, Lock)
            End If
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).ReadOnly = Lock
            End If
        Next
    End Sub

Call like this

    LockFields(Me, True)

or

    LockFields(Me, False)

Roger

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

Ask the Experts

Author

Commented:
Oops. Forgot to award the points. Sorry.
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.

OR

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.