question

DucNguyen-0389 avatar image
0 Votes"
DucNguyen-0389 asked Mike-4515 published

How do I do a loop for this ?

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
dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DucNguyen-0389 avatar image
0 Votes"
DucNguyen-0389 answered DucNguyen-0389 commented

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

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


What actions do you want to perform by the loop (incrementing labels’ values?) and how many times?

0 Votes 0 ·

Yes, incrementing labels’ values. But I'm not sure what you meant by how many time. Let's say forever, can I do that?

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered DucNguyen-0389 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Sorry, that's not what I want. I want all buttons to control the value of the textboxes and the code that I posted before for each button is quite similar, I only have to change the number (for example btnAdd1, btnAdd2, lblQty1, lblQty2). So I think probably there is a shorter way to do it (like a loop or something like that)

0 Votes 0 ·
Mike-4515 avatar image
0 Votes"
Mike-4515 answered Mike-4515 published

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.