-->

TableLayoutPanel GetCellPosition and GetPositionFr

2019-02-25 23:39发布

问题:

This may seem a trite question but I find the Microsoft documentation on these methods lacking in any detail.

What is the difference between TablelLayoutPanel.GetCellPosition(Control control) and TableLayoutPanel.GetPositionFromControl(Control control) ?

I'm using .NET Framework 4

回答1:

GetCellPosition gets the declared position of the control, where as GetPositionFromControl gets the actual position the TableLayoutPanel has decided for the control. These are the same in most cases. Programatically set several controls to the same cell (or overlapping cells with ColumnSpan or RowSpan,) and see how the results differ.

Add a TableLayoutPanel to a Form and copy the following code. Run it and click on a Label to see the difference.

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Index As Integer = 0 To 4
            Dim Control As New Label
            Control.Text = String.Format("Control {0}", Index)
            AddHandler Control.Click, AddressOf Control_Click
            TableLayoutPanel1.Controls.Add(Control, 0, 0)
        Next
    End Sub

    Private Sub Control_Click(sender As Object, e As EventArgs)
        Dim Pos1 As TableLayoutPanelCellPosition = TableLayoutPanel1.GetPositionFromControl(sender)
        Dim Pos2 As TableLayoutPanelCellPosition = TableLayoutPanel1.GetCellPosition(sender)
        Dim Text As String = String.Format("GetPositionFromControl = {0},{1}" & vbCrLf & "GetCellPosition = {2},{3}", Pos1.Column, Pos1.Row, Pos2.Column, Pos2.Row)
        MessageBox.Show(Text)
    End Sub
End Class