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

TablelLayoutPanel.GetCellPosition(Control control)TableLayoutPanel.GetPositionFromControl(Control control)有什么区别?

我正在使用.NET Framework 4

最佳答案

GetCellPosition获取控件的声明位置,而GetPositionFromControl获取控件的实际位置。在大多数情况下,这些都是相同的。以编程方式将多个控件设置到同一单元格(或与TableLayoutPanelColumnSpan重叠的单元格),并查看结果如何不同。

将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

09-04 06:59
查看更多