Search code examples
vb.netbuttonshort

How to address lots of buttons in vb.net in shortest code


I am making a TicTacToe Program in Vb.net and it is essentially 9 games into one "big" game. So I have 81 buttons I want to disable able the winner is determined. How can I address them all in the shortest amount of code?

Private Sub CheckOverallWinner()
If humangame1 = 1 And humangame2 = 1 And humangame3 = 1 Then
MsgBox("Human Wins")
Button1.Enabled = False
Button2.Enabled = False
Button3.Enabled = False
Button4.Enabled = False
Button5.Enabled = False
Button6.Enabled = False
Button7.Enabled = False
Button8.Enabled = False
Button9.Enabled = False
End If
End Sub

So instead of writing Button_.Enabled = False all the way through 81 is there a shorter way? Thanks!


Solution

  •     Dim o As Object
        For Each o In Me.Controls
            If TypeOf o Is Button Then
                o.Enabled = False
            End If
        Next
    

    Just tried this in VS2008