Friday, February 18, 2011

VB notes 2-18-11 Loops and Do Whiles

To count the number of check boxes checked on the form. 3 Check boxes on the form.


Dim nCount as integer = 0
for each element as object in me.controls
If TypeOf element Is CheckBox Then
If CType(element, CheckBox).Checked = True Then
nCount +=1
End If
End If
Next
txtCount.Text = nCount

Dim nControls As Integer = me.Controls.Count
Dim nCurrent As Integer = 0
Dim obj As Object
Do While nCurrent < nControls

obj = Me.Controls.Item(nCurrent)
If TypeOf ojb Is CheckBox Then
If CType(obj, CheckBox).Checked Then
nCount +=1
End If
End If
nCurrent +=1
txtCount.Text = nCount
If nCount > 1 Then
Exit Do
Loop

No comments:

Post a Comment