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

For each and form controls

P: n/a
win forms - xp pro - vb 2005

Shouldn't this code look at all textboxes on my form and make it lightblue
and say "OK"?
It seems to be evaluating a button I have on the form and giving me this
error

"A first chance exception of type 'System.InvalidCastException' occurred in
Math.exe"
For Each Textbox As System.Windows.Forms.TextBox In Me.Controls
Textbox.BackColor = System.Drawing.Color.LightBlue
Textbox.Text = "OK"
Next Textbox

1st line of trace stack -- which shows that it looked at the button
at Math.test.CheckAnswers_Click(Object sender, EventArgs e) in
C:\Math\Math\test.vb:line 67

thanks for your help
Tony

Apr 6 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
TonyMast wrote:
win forms - xp pro - vb 2005

Shouldn't this code look at all textboxes on my form and make it lightblue
and say "OK"?
It seems to be evaluating a button I have on the form and giving me this
error

"A first chance exception of type 'System.InvalidCastException' occurred in
Math.exe"
For Each Textbox As System.Windows.Forms.TextBox In Me.Controls
Textbox.BackColor = System.Drawing.Color.LightBlue
Textbox.Text = "OK"
Next Textbox

1st line of trace stack -- which shows that it looked at the button
at Math.test.CheckAnswers_Click(Object sender, EventArgs e) in
C:\Math\Math\test.vb:line 67

thanks for your help
Tony


No, that tries to convert every control in the form to a textbox.

For Each C As Control In Me.Controls
if typeof C is TextBox then
Dim T as Textbox
T.BackColor = System.Drawing.Color.LightBlue
T.Text = "OK"
end if
Next C

Chris
Apr 6 '06 #2

P: n/a
Tony,

Beside the answer from Chris, should you be aware that it does all textboxes
direct on your form.

A textbox that is in another control in your form is not done.

Have a look at this sample how to do recursive.
http://www.vb-tips.com/default.aspx?...6-56e3599238c1

Only the top of the sample.

I hope this helps,

Cor
"TonyMast" <To******@xxxMSNxxx.Com> schreef in bericht
news:uK**************@TK2MSFTNGP04.phx.gbl...
win forms - xp pro - vb 2005

Shouldn't this code look at all textboxes on my form and make it lightblue
and say "OK"?
It seems to be evaluating a button I have on the form and giving me this
error

"A first chance exception of type 'System.InvalidCastException' occurred
in Math.exe"
For Each Textbox As System.Windows.Forms.TextBox In Me.Controls
Textbox.BackColor = System.Drawing.Color.LightBlue
Textbox.Text = "OK"
Next Textbox

1st line of trace stack -- which shows that it looked at the button
at Math.test.CheckAnswers_Click(Object sender, EventArgs e) in
C:\Math\Math\test.vb:line 67

thanks for your help
Tony

Apr 6 '06 #3

P: n/a
Op Wed, 05 Apr 2006 21:23:50 -0400 schreef Chris:
No, that tries to convert every control in the form to a textbox.

For Each C As Control In Me.Controls
if typeof C is TextBox then
Dim T as Textbox
T.BackColor = System.Drawing.Color.LightBlue
T.Text = "OK"
end if
Next C

Chris


Chris,

I also always forget to assign C to T:-)

Renze de Waal.
Apr 6 '06 #4

P: n/a
"Renze de Waal" <re***@dewaal.speedlinq.nl> wrote in message
news:e8****************************@40tude.net...
Op Wed, 05 Apr 2006 21:23:50 -0400 schreef Chris:
No, that tries to convert every control in the form to a textbox.

For Each C As Control In Me.Controls
if typeof C is TextBox then
Dim T as Textbox
T.BackColor = System.Drawing.Color.LightBlue
T.Text = "OK"
end if
Next C

Chris


Chris,

I also always forget to assign C to T:-)

Renze de Waal.


fwiw... VB6 version...

Dim C As Control
For Each C In Controls
if typeof C is TextBox then
C.BackColor = LightBlue 'your defined constant
C.Text = "OK"
end if
Next


There'd be no reason to create and assign an additional object variable....
unless you want intellisense.
--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Apr 6 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.