Results 1 to 4 of 4

Thread: [RESOLVED] Iterate through buttons using loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2018
    Posts
    2

    Resolved [RESOLVED] Iterate through buttons using loop

    Hello all,

    I have got nine buttons on my applications which I want my loop to run through and change text of the button to an empty string.

    At the begining of a sub I've got buttons declared as buttons. Followed by loop that i ranges from 1 to 9 and every loop code changes button text and makes it enabled again.

    I could just hard code these two action for every button but I dont think it is smart coding devision to do. For example:

    Button1.text = ""
    Button1.Enabled = True
    ...
    ...
    Button9.text = ""
    Button9.Enabled = True

    Is there a simple solution for this?

    Code:
        Private Sub end_game()
            Dim b1 As Button = Button1
            Dim b2 As Button = Button2
            Dim b3 As Button = Button3
            Dim b4 As Button = Button4
            Dim b5 As Button = Button5
            Dim b6 As Button = Button6
            Dim b7 As Button = Button7
            Dim b8 As Button = Button8
            Dim b9 As Button = Button9
    
            For i As Byte = 1 To 9
                b(i).text = ""
                b(i).enabled = True
            Next i
    
        End Sub

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Iterate through buttons using loop

    Rather than creating separate variables for the buttons, you can create an array of them:
    Code:
        Private Sub end_game()
            Dim b As Button() = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
    
            For i As Byte = 1 To 9
                b(i).text = ""
                b(i).enabled = True
            Next i
    
        End Sub
    Also note that using Byte for the data type of a loop counter isn't the best choice... while it uses slightly less memory than other data types (a negligible amount, even if you have hundreds of nested loops running), it makes the code run more slowly than with larger data types. Integer is generally best, as that is designed to be the same size as the processor is optimised for.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Iterate through buttons using loop

    As suggested, you can create an array directly. If these Buttons are the only Buttons in their parent container, you can also do this:
    vb.net Code:
    1. For Each btn In parentContainer.Controls.OfType(Of Button)()
    2.     '...
    3. Next

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Iterate through buttons using loop

    Indeed... and you have reminded me of something I should have pointed out before, which is that a For Each loop in my example would have made the code easier to read:
    Code:
        Private Sub end_game()
            Dim buttons As Button() = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9}
    
            For Each b in buttons
                b.text = ""
                b.enabled = True
            Next
    
        End Sub

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