|
-
Dec 5th, 2002, 05:03 AM
#1
Thread Starter
Member
Do ... While problem
Why doesn't the yellow button do what its supposed to do? Is my do, while statement incorrect? The blue and red work. Help! Thank you.
VB Code:
Private Sub BlueButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueButton.Click
Dim objControl As Control
For Each objcontrol In Controls
If TypeOf objcontrol Is Label Then
objControl.BackColor = BackColor.Blue
End If
Next objControl
End Sub
Private Sub YellowButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles YellowButton.Click
Dim objControl As Control
Do While TypeOf objControl Is Label
objControl.BackColor = BackColor.Yellow
Loop
End Sub
Private Sub RedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RedButton.Click
Dim LabelCollection As New Collection()
Dim objControl As Control
For Each objControl In Controls
If TypeOf objControl Is Label Then
LabelCollection.Add(objControl)
End If
Next objControl
For Each objControl In LabelCollection
objControl.BackColor = BackColor.Red
Next objControl
End Sub
-
Dec 5th, 2002, 06:02 AM
#2
Member
hi
for (each)...next increments automatically the variable (in your example objControl)
do... loop does NOT increment automatically
for i = 0 to 10
messagebox.show(i)
next
you do not have to increment i
do while i <= 10
messagebox.show(i)
i = i +1
loop
without the statement i = i +1 do...loop will never end because i is not incremented automatically
-
Dec 5th, 2002, 10:40 AM
#3
Thread Starter
Member
So then what I have below should work, right? Well it doesn't. It crashes the program when I click on the yellow button.
VB Code:
'runs a loop checking for all labels then changes them to yellow
Private Sub YellowButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles YellowButton.Click
Dim objControl As Control
Dim i As Integer = 1
Do While i <= 10
If TypeOf objControl Is Label Then
objControl.BackColor = BackColor.Yellow
End If
Loop
End Sub
-
Dec 5th, 2002, 11:30 AM
#4
First off objControl will never be a label because it is declared as control and never set to anything else. In the other you make use of the controls collection to assign objControl to a control but here you don't.
VB Code:
Dim objControl As Control 'you are declaring it as a control but it is never set to anything else
Dim i As Integer = 1
Do While i <= 10
If TypeOf objControl Is Label Then
objControl.BackColor = BackColor.Yellow
End If
Loop
I think this is what you mean:
VB Code:
Dim objControl As Control
For Each objcontrol In Controls
If TypeOf objcontrol Is Label Then
objControl.BackColor = BackColor.Yellow
End If
Next
-
Dec 5th, 2002, 11:44 AM
#5
Thread Starter
Member
Ok, I see what you're saying. I need to use a Do...While...Loop however. How can I do that?
-
Dec 5th, 2002, 12:04 PM
#6
Why do you need to use a Do..While Loop? What is the code suppose to do? If it is just suppose to change all the labels in the controls collection to yellow then a do while loop isn't really what you want to use.
-
Dec 5th, 2002, 12:05 PM
#7
Thread Starter
Member
Because that's what the homework is :-D
-
Dec 5th, 2002, 02:14 PM
#8
Thread Starter
Member
So how would I use a do while loop to change the backcolor of these labels?
-
Dec 5th, 2002, 04:38 PM
#9
Registered User
Code:
Dim counter As Integer = 0
Do While counter <= Controls.Count - 1
If TypeOf Controls(counter) Is Label Then
Controls(counter).BackColor = Color.Yellow
End If
counter += 1
Loop
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|