Home All Groups Group Topic Archive Search About

For each and form controls

Author
6 Apr 2006 2:05 AM
TonyMast
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

Author
6 Apr 2006 1:23 AM
Chris
TonyMast wrote:
Show quoteHide quote
> 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
Author
6 Apr 2006 5:57 PM
Renze de Waal
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.
Author
6 Apr 2006 10:21 PM
Ken Halter
Show quote Hide quote
"Renze de Waal" <re***@dewaal.speedlinq.nl> wrote in message
news:e8njr3q537fh.w3yiwnwonqtq$.dlg@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..
Author
6 Apr 2006 6:31 AM
Cor Ligthert [MVP]
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?ID=8ff290d4-5c16-4cef-ba06-56e3599238c1

Only the top of the sample.

I hope this helps,

Cor


Show quoteHide quote
"TonyMast" <TonyM***@xxxMSNxxx.Com> schreef in bericht
news:uKRMW6RWGHA.1900@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
>
>
>