473,473 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

For each and form controls

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
4 9528
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lauren Quantrell | last post by:
In terms of resources, wondering if it makes a difference in adopting one of the three options below for resizing controls on a form and controls on a subform at the same time. option a.) Put...
7
by: J L | last post by:
I need to loop through a form's controls collection and delete some based on type and location. I have tried For..Each and For i = 0 to form.controls.count - 1 but when I delete any it messes up...
4
by: Dries De Rudder | last post by:
Hi, I am trying to bind an object to form controls. I've got an object MyObject which has some properties e.g. ID. I've got a form, Form1, that contains a textbox ,TextBox1, now I want to...
2
by: Adam Right | last post by:
Hi, I want to get the controls of the windows form. I know that form.Controls give me the list of controls but i have a problem. For example, the toolstripmenu control cannot be reached with...
1
by: Adam Right | last post by:
Hi, I want to get the controls of the windows form. I know that form.Controls give me the list of controls but i have a problem. For example, the toolstripmenu control cannot be reached with...
7
by: jmp | last post by:
(I hope this isn't considered too far off-topic.) I work as a developer on a browser product found on handheld devices, and I'm trying to specify behavior for the browser to make it "as...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
9
by: dhtml | last post by:
I have written an article "Unsafe Names for HTML Form Controls". <URL: http://jibbering.com/faq/names/ > I would appreciate any reviews, technical or otherwise. Garrett --...
10
by: =?Utf-8?B?UmljaA==?= | last post by:
A lot of users at my workplace use different screen resolutions, and I build apps to use 1680 x 1050 pixels res by default. But some users are using 800 x 600, and the apps are too large for their...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.