Share via

How do I do a loop for this ?

Duc Nguyen 101 Reputation points
2021-05-11T07:12:38.413+00:00

Private Sub btnAdd1_Click(sender As Object, e As EventArgs) Handles btnAdd1.Click
lblQty1.Text += 1
End Sub

Private Sub btnAdd4_Click(sender As Object, e As EventArgs) Handles btnAdd4.Click
    lblQty2.Text += 1
End Sub

Private Sub btnAdd7_Click(sender As Object, e As EventArgs) Handles btnAdd7.Click
    lblQty3.Text += 1
End Sub
Developer technologies | VB
0 comments No comments

3 answers

Sort by: Most helpful
  1. Mike 1 Reputation point
    2021-05-11T12:24:15.427+00:00

    Something like this might be what you are looking for:

    Public Shared Function CycleControls(ByVal Page As Control) As Boolean

        'this function cycles through all the labels on a web form 
    
    
        Try
    
            For Each ctrl As Control In Page.Controls
    
                If TypeOf ctrl Is Label Then
                    CType(ctrl, Label).Text = CType(ctrl, Label).Text + 1                   
                End If
    
    
            Next
            Return True
        Catch
            Return False
        End Try
    
    End Function
    

    Was this answer helpful?

    0 comments No comments

  2. Viorel 127K Reputation points
    2021-05-11T09:39:53.74+00:00

    To increment the labels, drag a Timer control to your form in Form Designer, set Enabled to True and Interval to 1000, add the Tick handler (double-click the added component), where you can increment your labels, for example:

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       lblQty1.Text += 1
       lblQty2.Text += 1
       lblQty3.Text += 1
    End Sub
    

    Was this answer helpful?


  3. Duc Nguyen 101 Reputation points
    2021-05-11T07:41:49.37+00:00

    Sorry, the code is like this:

    Private Sub btnAdd1_Click(sender As Object, e As EventArgs) Handles btnAdd1.Click
    lblQty1.Text += 1
    End Sub

    Private Sub btnAdd2_Click(sender As Object, e As EventArgs) Handles btnAdd2.Click
    lblQty2.Text += 1
    End Sub
    Private Sub btnAdd3_Click(sender As Object, e As EventArgs) Handles btnAdd3.Click
    lblQty3.Text += 1
    End Sub

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.