By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,434 Members | 2,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,434 IT Pros & Developers. It's quick & easy.

Disable all text boxes on form at once

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* "Brian Henry" <br**********@newsgroups.nospam> scripsit:
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?


<URL:http://dotnet.mvps.org/dotnet/samples/controls/downloads/EnumerateControls.zip>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2

P: n/a
"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
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

--
Sven Groot

Nov 20 '05 #3

P: n/a
"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
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.
Nov 20 '05 #4

P: n/a
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
////
Nov 20 '05 #5

P: n/a
"Brian Henry" <br**********@newsgroups.nospam> schrieb
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

Nov 20 '05 #6

P: n/a
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
Nov 20 '05 #7

P: n/a
oh yes, the is.. dumb me forgetting about types
"Sven Groot" <sv*******@gmx.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:ed**************@tk2msftngp13.phx.gbl...
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

--
Sven Groot

Nov 20 '05 #8

P: n/a
"Armin Zingler" <az*******@freenet.de> schrieb
[bla]

sorry for sounding like an echo... ;)
--
Armin
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.