TableLayoutPanel GetCellPosition和GetPositionFromControl,它们有何不同?

时间:2011-08-22 00:56:13

标签: c# .net vb.net winforms tablelayoutpanel

这似乎是一个陈腐的问题,但我发现有关这些方法的Microsoft文档没有任何细节。

TablelLayoutPanel.GetCellPosition(Control control)TableLayoutPanel.GetPositionFromControl(Control control)之间的区别是什么?

我正在使用.NET Framework 4

1 个答案:

答案 0 :(得分:3)

GetCellPosition获取控件的声明位置,其中GetPositionFromControl获取TableLayoutPanel为控件决定的实际位置。在大多数情况下,这些都是相同的。以编程方式将多个控件设置为同一个单元格(或使用ColumnSpanRowSpan重叠单元格),并查看结果的不同。

将TableLayoutPanel添加到表单并复制以下代码。运行它并单击标签以查看差异。

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