Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For...Each loop

Status
Not open for further replies.

fattyfatpants

Programmer
Apr 22, 2004
68
US
Good morning Gurus, I was wondering if I may enlist your help to solve a problem that I am having....

I am new to .NET but have been coding VB6 for about 2.5 years and am wondering how to do a for...each loop to iterate through all the controls on a form

as you know, in VB6 the code is something like
dim ctl as control
.
.
.
for each ctl in me.controls.count
if typeof ctl is TextBox then
do something
elseif typeof ctl is CheckBox then
do something
end if
next

how can I adapt this type of code to work with VB.NET?? Any suggestions would be greatly appreciated
 
The exact controls that i want to find out the status of are CheckBoxes located in a groupbox, so my code is as follows:

Dim ctl as Control
Dim sngSubTotal

For Each ctl in grpToppings.Controls
If TypeOf ctl is CheckBox Then
If ctl.Checked = True Then
sngSubTotal += CSng("1.5")
End If
End If
Next

When I leave this For...Each loop the ctl.Checked portion is underlined saying that Checked is not a member of System.Windows.Forms.Control...What gives here??
 
Never mind I figured it out...thanks anyway

for those who didn't know here's my code

Dim ctl as Windows.Forms.Control
Dim chkBox as Windows.Forms.CheckBox
Dim sngSubTotal as Single

For Each ctl in grpToppings.Controls
If ctl.GetType Is GetType(Windows.Forms.CheckBox) Then
chkBox = DirectCast(ctl, Windows.Forms.CheckBox)
If chkBox.Checked = True Then
sngSubTotal += CSng("1.5")
End If
End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top