В проге создаю массив кнопак
Код | Private buttonArray As Button(,)
Public Function ButtonsArrayNewAllocation() As Button(,) Dim buttonArray As Button(,) = New Button(gameSize - 1, gameSize - 1) {} Dim row As Byte, col As Byte For row = 0 To buttonArray.GetLength(0) - 1 For col = 0 To buttonArray.GetLength(1) - 1 buttonArray(row, col) = New Button() Next Next Return buttonArray End Function
|
и помимо всего прочего прописываю два события:
Код | Private Sub ButtonsArrayMouseEnter(ByVal sender As Object, ByVal e As EventArgs) Dim buttonTag As Integer = CInt((DirectCast(sender, Button).Tag)) Dim row As Integer = (buttonTag / gameSize) + 1 Dim col As Integer = (buttonTag Mod gameSize) + 1 For i As Integer = 0 To gameSize - 1 If buttonArray(row, i).Text = "X" Then buttonArray(row, i).BackColor = Color.LightSkyBlue End If If buttonArray(i, col).Text = "X" Then buttonArray(i, col).BackColor = Color.LightSkyBlue End If Next If row = col Then For i As Integer = 0 To gameSize - 1 If buttonArray(i, i).Text = "X" Then buttonArray(i, i).BackColor = Color.LightSkyBlue End If Next End If If row + col = gameSize - 1 Then For i As Integer = 0 To gameSize - 1 If buttonArray(i, gameSize - i - 1).Text = "X" Then buttonArray(i, gameSize - i - 1).BackColor = Color.LightSkyBlue End If Next End If End Sub
Private Sub ButtonsArrayMouseLeave(ByVal sender As Object, ByVal e As EventArgs) Dim buttonTag As Integer = CInt((DirectCast(sender, Button).Tag)) Dim row As Integer = buttonTag / gameSize Dim col As Integer = buttonTag Mod gameSize For i As Integer = 0 To gameSize - 1 buttonArray(row, i).BackColor = Color.Yellow buttonArray(i, col).BackColor = Color.Yellow Next If row = col Then For i As Integer = 0 To gameSize - 1 buttonArray(i, i).BackColor = Color.Yellow Next End If If row + col = gameSize - 1 Then For i As Integer = 0 To gameSize - 1 buttonArray(i, gameSize - i - 1).BackColor = Color.Yellow Next End If End Sub
|
прога компилируется! запускается! но после какогота времяни хождения курсора по кнопкам комп выкидывает ошибку : "Индекс вышел за границы массива!" Что не так |