我在vb.net中有10 x 10 tablelayoutpanel和10 x 10数组。我希望能够用鼠标单击一个单元格,并且该特定单元格将更改颜色,相应的数组元素将为1.未单击的单元格将在数组中具有0,依此类推。单击另一个单元格,单元格会更改颜色,相应的数组元素会更新。
在单元格上单击鼠标按钮后,我不确定如何更改tableLayoutPanel中单个单元格的颜色。
尝试通过谷歌搜索寻找想法,但似乎无法找到具体的东西。任何帮助将不胜感激。
答案 0 :(得分:0)
细胞不是对照,不能单独控制背面颜色。您可以在每个单元格中放置一个面板,并在运行时添加处理程序。此示例将切换TLP中每个单元格的红色和蓝色之间的颜色
Private panels As New Dictionary(Of Panel, Point)()
Private values(9, 9) As Integer
Private Sub PanelClick(sender As Object, e As EventArgs)
Dim s = DirectCast(sender, Panel)
Dim x = panels(s).X
Dim y = panels(s).Y
values(x, y) = If(values(x, y) = 0, 1, 0)
s.BackColor = If(values(x, y) = 0, Color.Red, Color.Blue)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 0 To TableLayoutPanel1.ColumnCount - 1
For j = 0 To TableLayoutPanel1.RowCount - 1
Dim s = TableLayoutPanel1.GetControlFromPosition(i, j)
If s IsNot Nothing Then
Dim p = DirectCast(TableLayoutPanel1.GetControlFromPosition(i, j), Panel)
panels.Add(p, New Point(i, j))
AddHandler p.Click, AddressOf PanelClick
End If
Next
Next
End Sub