Lock Text Boxes (VB 2005)

Posted on 2006-03-24
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'.
Question by:bhlang
    LVL 52

    Accepted Solution

    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

    Author Comment

    Thanks. Knew it was something simple, but couldn't figure out what.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now