0

对于我的家庭作业,我被要求创建一个由按钮组成的棋盘。此棋盘格的大小由用户通过文本框确定。棋盘中按钮的颜色在红色和蓝色之间交替。如果用户单击其中一个按钮,它必须将其颜色更改为红色或蓝色(取决于当前颜色)。此外,单击按钮必须切换该行中每个按钮的颜色。

我的颜色数组包含两个维度(rowInteger, colInteger),它们使用用户输入的文本框值。

我单击动态创建的按钮之一的代码如下:

Protected Sub tempBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim thisButton As Button = CType(sender, Button)

    For i As Integer = color.GetLowerBound(0) To color.GetUpperBound(0)
        For j As Integer = color.GetLowerBound(1) To color.GetUpperBound(1)
            If thisButton Is color(i, j) Then
                color(i, j).BackColor = Drawing.Color.Black
            End If
        Next
    Next
End Sub

请注意,出于测试目的,我将颜色严格更改为黑色

我创建按钮的代码如下:

Private Sub createTable()
    Dim tempBtn As Button
    checkerBoard.BorderStyle = BorderStyle.Groove
    checkerBoard.BorderWidth = Unit.Pixel(2)
    checkerBoard.Controls.Clear()

    For rowInteger = 0 To CInt(rowTxtBox.Text) - 1
        Dim newTableRow As New TableRow
        checkerBoard.Controls.Add(newTableRow)

        For colInteger = 0 To CInt(colTxtBox.Text) - 1
            Dim newTableCell As New TableCell
            tempBtn = New Button
            If colInteger Mod 2 = rowInteger Mod 2 Then
                tempBtn.BackColor = Drawing.Color.Red
            Else
                tempBtn.BackColor = Drawing.Color.Blue
            End If

            tempBtn.Width = 200
            tempBtn.Height = 200
            AddHandler tempBtn.Click, AddressOf tempBtn_Click
            newTableCell.Controls.Add(tempBtn)
            newTableRow.Controls.Add(newTableCell)

            ReDim color(rowInteger, colInteger)
            color(rowInteger, colInteger) = tempBtn
        Next
    Next
End Sub

当我运行我的网站并单击一个按钮时,它的颜色变得透明。为什么会是这样的结果?我可以更改单个按钮的颜色(通过更改 thisButton 的颜色),但是如果我尝试使用上面的代码更改其颜色,它根本不起作用。

编辑:几个小时后再次运行后,颜色根本没有改变。我相信我的解决方案可能有错误,但我不知道它是什么

4

0 回答 0