你可以試試這個功能:
Private Function GetCellFromPoint(p As Point) As Point
Dim result As New Point(-1, -1)
Dim colWidths As Integer() = tlp.GetColumnWidths()
Dim rowHeights As Integer() = tlp.GetRowHeights()
Dim top As Integer = 0
For y As Integer = 0 To rowHeights.Length - 1
Dim left As Integer = 0
For x As Integer = 0 To colWidths.Length - 1
If New Rectangle(left, top, colWidths(x), rowHeights(y)).Contains(p) Then
result = New Point(x, y)
End If
left += colWidths(x)
Next
top += rowHeights(y)
Next
Return result
End Function
它只是通過行和列循環,看看是否在傳遞點是現有的細胞內。請注意,GetColumnWidths
和GetRowHeights
未顯示在智能感知下拉列表中。
用法:
Private Sub tlp_MouseMove(sender As Object, e As MouseEventArgs) _
Handles tlp.MouseMove
Me.Text = GetCellFromPoint(e.Location).ToString
End Sub
順便說一句,GetCellPosition
期待作爲參數,而不是一個點結構要傳遞的控制。
您也可以使用TableLayoutPanelCellPosition
來代替此函數中的Point,因爲GetCellPosition正是在其函數中返回的。