Následující kód zjistí na jaké buňce bylo kliknuto a vybarví ji na červenou
Private _Clicked As Boolean
Private Sub TableLayoutPanel1_CellPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.TableLayoutCellPaintEventArgs) Handles TableLayoutPanel1.CellPaint
If _Clicked AndAlso ColInd = e.Column AndAlso RowInd = e.Row Then
e.Graphics.FillRectangle(Brushes.Red, e.CellBounds)
_Clicked = False
End If
End Sub
Dim RowInd, ColInd As Integer
Private Sub TableLayoutPanel1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TableLayoutPanel1.MouseClick
Dim ColPos As Integer
Dim RowPos As Integer
Label3.Text = e.Location.ToString
Dim ColWidths(), RowHeights() As Integer
ColWidths = TableLayoutPanel1.GetColumnWidths()
RowHeights = TableLayoutPanel1.GetRowHeights()
'zjisteni aktualni bunky
For i = 0 To ColWidths.Length - 1
ColPos = ColPos + ColWidths(i)
If e.X < ColPos Then
ColInd = i
Exit For
End If
Next
For i = 0 To RowHeights.Length - 1
RowPos = RowPos + RowHeights(i)
If e.Y < RowPos Then
RowInd = i
Exit For
End If
Next
Label4.Text = New Point(ColInd, RowInd).ToString
_Clicked = True
TableLayoutPanel1.Refresh()
End Sub
při dalším kliknutí se barva vrátí zpět. Aby buňka zůstala obarvena, v _MouseClick by se musela ukládat vazba ColInd,RowInd,barva
|