Disable all text boxes on form at once

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

how would you disable all text boxes on a form at once/

I thought

dim tx as textbox
for each tx in me.controls
tx.enabled = false
next

but that crashes, what do you guys suggest? thanks
 
Brian Henry said:
how would you disable all text boxes on a form at once/

I thought

dim tx as textbox
for each tx in me.controls
tx.enabled = false
next

but that crashes, what do you guys suggest? thanks

Dim c As Control
For Each c In Me.Controls
If TypeOf c Is TextBox Then
c.Enabled = False
End If
Next
 
Brian Henry said:
how would you disable all text boxes on a form at once/

dim tx as textbox
for each tx in me.controls

This will /only/ work if /every/ control on the form is a TextBox.
Otherwise, it will get upset trying to cast, say, a Label into your
TextBox variable.

Try this (I'm using VB'2003 here)

For Each ctl as Control in Me.Controls
If ctl Is TextBox Then
ctl.Enabled = False
End If
Next

HTH,
Phill W.
 
Hi Brian,

The one I like, I typed it in here so watch typos

I hope this helps

Cor

\\\\
Private sub Set
doset(Me)
end sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
if typeof ctr is textbox then
ctr.enabled = false
end if
doSet(ctr)
Next
End Sub
////
 
Brian Henry said:
how would you disable all text boxes on a form at once/

I thought

dim tx as textbox
for each tx in me.controls
tx.enabled = false
next

but that crashes, what do you guys suggest? thanks

Not all controls on the Form are textboxes, so whenever, within the loop, a
control is assigned to 'tx' that is not a textbox, an InvalidCastException
occurs. Instead:

dim o as object
for each o in me.controls
if typeof o is textbox then
directcast(o, textbox).enabled = false
end if
next

Note that this only processes the Textboxes directly placed on the Form, not
those eg placed on a groupbox on the Form.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Hi Armin,
Note that this only processes the Textboxes directly placed on the Form, not
those eg placed on a groupbox on the Form.

Have a look at that nice routine I made for it.
(It is one of them of course, however this one I like specially in this
situation, it is very good when you have to set one handler for all
controls)

Cor
 
oh yes, the is.. dumb me forgetting about types


Sven Groot said:
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is TextBox Then
c.Enabled = False
End If
Next
 
Back
Top