Search code examples
vb.netforms

How to Enable/Disable Multiple Buttons Simultaneously Without Writing Code For Each Individual Button?


I am currently coding a simple hangman game that has a button for each letter that the player can input as their guess. When the game ends I want all the Letter Buttons (a, b, c, d, etc.) to be disabled so the user cannot click on them until they press the start game button and a new word is generated. I was wondering if it's possible to disable all the letter buttons without writing this for each button: button1.enabled = False button2.enabled = False button3.enabled = False...

I was wondering if I could instead only write a few lines in comparison to 26 lines of code to achieve this.

I have tried this but it is very inefficient and I was wondering if there is a different option? button1.enabled = False button2.enabled = False button3.enabled = False...

The circled buttons are the ones I want to disable: (https://i.sstatic.net/2GUOO.png)

Thank you in advance, Georgie


Solution

  • as Hursey said in comment, create a panel and move all involved buttons in it with mouse.

    then you can loop thru children controls :

    For Each CtrlButton As Control In Panel1.Controls
        If TypeOf CtrlButton Is Button Then 'only buttons
            CtrlButton.Enabled = False
        End If
    Next