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

Home Posts Topics Members FAQ

For Each... statement

group,

please help with the following codes below.

why would this not work... would it require casting/reflecting? if so, does
anyone show me the right codes?

Dim chk As System.Windows.Forms.CheckBox
If Not MsgBox("Are you sure you wish to clear?", MsgBoxStyle.YesNo +
MsgBoxStyle.Exclamation, "Clearing") = MsgBoxResult.Yes Then Exit Sub
For Each ctl In Me.Controls
If TypeName(ctl) = "GroupBox" Then
For Each chk In ctl.Controls
chk.Checked = False
Next
End If
Next
Please help.
Thanks in advance.
Ronin
Mar 16 '06 #1
3 1195
why would this not work...


The enumerator returns all controls regardless of the type of your
iteration variable (chk As CheckBox in this case).

You have to do something like

For Each chk As Control In ctl.Controls
If TypeOf chk Is CheckBox Then CType(chk, CheckBox).Checked =False
Next

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 16 '06 #2
"Ronin" <Ro***@discussions.microsoft.com> schrieb
group,

please help with the following codes below.

why would this not work... would it require casting/reflecting? if
so, does anyone show me the right codes?

Dim chk As System.Windows.Forms.CheckBox
If Not MsgBox("Are you sure you wish to clear?",
MsgBoxStyle.YesNo + MsgBoxStyle.Exclamation, "Clearing") =
MsgBoxResult.Yes Then Exit Sub
For Each ctl In Me.Controls
If TypeName(ctl) = "GroupBox" Then
For Each chk In ctl.Controls
chk.Checked = False
Next
End If
Next

I tried it and it works here.

"Not work" means what? Compile error? Exception? Maybe either the groupbox
is not directly placed on the Form, or not all controls in the Groupbox are
checkboxes. If the latter is the case, use

For Each ctl2 as Control In ctl.Controls
if typeof ctl2 is Checkbox then
dim chk as checkbox=directcast(ctl2, checkbox)
chk.Checked = False
end if
Next

BTW, I would use

If typeof ctl is GroupBox

instead, because the compiler would discover a misspelled type name. But
this is not the reason for the problem.
Armin

Mar 16 '06 #3
"Ronin" <Ro***@discussions.microsoft.com> wrote in message
news:23**********************************@microsof t.com...
why would this not work... would it require casting/reflecting? if so,
does
anyone show me the right codes?


Something like this :

Option Strict On
Imports System.Windows.Forms

.. . .

If MsgBox("Are you sure you wish to clear?" _
, MsgBoxStyle.YesNo Or MsgBoxStyle.Question _
, "Clearing") = MsgBoxResult.Yes _
Then
For Each possibleGB As Control In Me.Controls
If TypeOf( possibleGB ) Is GroupBox Then
For Each possibleCB As Control In ctl.Controls
If TypeOf( possibleCB ) Is CheckBox Then
DirectCast( possibleCB, Checkbox).Checked = False
End If
Next
End If
Next

End If

HTH,
Phill W.
Mar 22 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: deko | last post by:
Problem: why doesn't this With block work? is it possible to have a For Each loop within a With block? With objWord .Documents.Add Template:=strTemplate, NewTemplate:=False, DocumentType:=0...
0
by: Frank van Vugt | last post by:
L.S. Postgresql version 7.4.3 does not allow declaration of a 'for each statement' constraint trigger: db=# \h create constraint Command: CREATE CONSTRAINT TRIGGER Description: define a...
14
by: Drew | last post by:
I need to iterate through a submitted form, inserting data on each pass. In the past, I have always used form elements that were named with numbers at the end, like this, name1 relationship1...
1
by: ll | last post by:
Hi all, I've inherited a site and am currently looking to redesign a page that displays a table of course curriculum, with each row representing a "Topic" of the curriculum. There is a courseID...
1
by: ChrisC | last post by:
Hello, I am attempting to create a trigger to keep track of changes to a table, mainly about specific changes to the data in the table. I had hoped that triggers defined as for each row would...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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.