Programmatically set all textboxes to readonly

brentman

Member
Joined
Jan 25, 2006
Messages
5
Location
Greensburg, PA
Programming Experience
3-5
I have an entry form that I would like to disable entry more and have it read-only. I seem to be missing the boat on dynamically controlling controls. I have been able to grab the name of a control, etc but never been able to change properties dynamically. Here is what I have, any help would be appreciated.

Dim c As Control
Dim txt As System.Windows.Forms.TextBox

For Each c In tabcontrol1.Controls
If TypeOf c Is TextBox Then
txt = CType(c, TextBox)
txt.ReadOnly =
True
End If

Next

This code does nothing at all. I assume it is because I am setting the property of a control-typed variable and not the actual control itself.

I am able to disable all controls by doing:
for each c in tabcontrol1.controls
c.enabled = false
next

But that isn't what I need. Any ideas? Thanks.
 
brentman said:
...This code does nothing at all. I assume it is because I am setting the property of a control-typed variable and not the actual control itself.
That assumption is wrong. Controls are reference types, so your variable references the original TextBox.
Problem is probably that your TextBoxes are placed on childcontrols (Panel, GroupBox, TabPages, or other containers). So you'll have to use recursion from the top container:
VB.NET:
[COLOR=black][COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] makeTextBoxesReadOnly([COLOR=blue]ByVal[/COLOR] control [COLOR=blue]As[/COLOR] Control)
    [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] item [COLOR=blue]As[/COLOR] Control [COLOR=blue]In[/COLOR] control.Controls
        [COLOR=blue]If[/COLOR] [COLOR=blue]TypeOf[/COLOR] item [COLOR=blue]Is[/COLOR] TextBox [COLOR=blue]Then[/COLOR]
            [COLOR=blue]DirectCast[/COLOR](item, TextBox).ReadOnly = [COLOR=blue]True[/COLOR]
        [COLOR=blue]Else[/COLOR]
            [COLOR=green]' recurse and make eventual TextBoxes [/COLOR]
            [COLOR=green]' on this control readonly[/COLOR]
            makeTextBoxesReadOnly(item)
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Next[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
[/COLOR]
 
If your textboxes were directly on the TabPage your code would work with a minor change, use the TabPage Control Collection instead of the TabControl control collection :

VB.NET:
[SIZE=2]

[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Control [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] TabPage1.Controls[/SIZE][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]TypeOf[/COLOR][/SIZE][SIZE=2] c [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2] TextBox [/SIZE][SIZE=2][COLOR=#0000ff]Then

[/COLOR][/SIZE][INDENT][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2](c, TextBox).ReadOnly = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE]
[/INDENT][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]

Of course this only work for one Tab Page at at time.
 
Thanks guys. Don Delegate I ended up using your recursive method. I hadn't thought considered that I have controls in a groupbox on a tabpage. Definitely not gonna get there without recursion. Still a winforms newbie.

Any ideas on disabling a combobox but still having a white background so it is easy to read? Setting enabled to false just greys it all out.
 
Setting the ReadOnly = True should get you there.... nto sure why that isn't working.

-tg
 
Note that you don't need to use recursion:
VB.NET:
    Private Sub MakeTextBoxesReadOnly()
        'Get the first control.
        Dim ctl As Control = Me.GetNextControl(Me, True)

        While Not ctl Is Nothing
            If TypeOf ctl Is TextBox Then
                DirectCast(ctl, TextBox).ReadOnly = True
            End If

            'Get the next control.
            ctl = Me.GetNextControl(ctl, True)
        End While
    End Sub
ComboBoxes have no ReadOnly property. They are enabled or disabled. If you want some other behaviour then you'd have to provide it yourself.
 
Only.... it's textboxes he's dealing with...

-tg
 
TechGnome said:
Only.... it's textboxes he's dealing with...

-tg
:rolleyes:
brentman said:
Any ideas on disabling a combobox but still having a white background so it is easy to read? Setting enabled to false just greys it all out.
;)
 
jmcilhinney said:
I must get my eyes checked..... I swear I thought it still said textboxes...

I sit corrected.

-tg
 
brentman said:
Is there a way to add my own property to the combobox class without creating my own control?
No, but all you have to do is create a class that inherits ComboBox and give it a single property. You don't have to reproduce any of the members of the ComboBox class as they will all simply be inherited. Having said that, it's going to take a bit more than just adding a property as you are trying to change its behaviour.
 
Back
Top