Results 1 to 9 of 9

Thread: Do ... While problem

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60

    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:
    1. Private Sub BlueButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlueButton.Click
    2.         Dim objControl As Control
    3.         For Each objcontrol In Controls
    4.             If TypeOf objcontrol Is Label Then
    5.                 objControl.BackColor = BackColor.Blue
    6.             End If
    7.         Next objControl
    8.  
    9.     End Sub
    10.  
    11.     Private Sub YellowButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles YellowButton.Click
    12.         Dim objControl As Control
    13.         Do While TypeOf objControl Is Label
    14.             objControl.BackColor = BackColor.Yellow
    15.         Loop
    16.     End Sub
    17.  
    18.     Private Sub RedButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles RedButton.Click
    19.         Dim LabelCollection As New Collection()
    20.         Dim objControl As Control
    21.         For Each objControl In Controls
    22.             If TypeOf objControl Is Label Then
    23.                 LabelCollection.Add(objControl)
    24.             End If
    25.         Next objControl
    26.         For Each objControl In LabelCollection
    27.             objControl.BackColor = BackColor.Red
    28.         Next objControl
    29.     End Sub

  2. #2
    Member
    Join Date
    Nov 2002
    Location
    Austria
    Posts
    52
    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

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    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:
    1. 'runs a loop checking for all labels then changes them to yellow
    2.     Private Sub YellowButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles YellowButton.Click
    3.         Dim objControl As Control
    4.         Dim i As Integer = 1
    5.         Do While i <= 10
    6.             If TypeOf objControl Is Label Then
    7.                 objControl.BackColor = BackColor.Yellow
    8.             End If
    9.         Loop
    10.     End Sub

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. Dim objControl As Control 'you are declaring it as a control but it is never set to anything else
    2.         Dim i As Integer = 1
    3.         Do While i <= 10
    4.             If TypeOf objControl Is Label Then
    5.                 objControl.BackColor = BackColor.Yellow
    6.             End If
    7.         Loop

    I think this is what you mean:
    VB Code:
    1. Dim objControl As Control
    2.         For Each objcontrol In Controls
    3.             If TypeOf objcontrol Is Label Then
    4.                 objControl.BackColor = BackColor.Yellow
    5.             End If
    6.         Next

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Ok, I see what you're saying. I need to use a Do...While...Loop however. How can I do that?

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    Because that's what the homework is :-D

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    60
    So how would I use a do while loop to change the backcolor of these labels?

  9. #9
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    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
  •  



Click Here to Expand Forum to Full Width