TableLayoutPanel GetCellPosition and GetPositionFromControl, how do they differ?

风流意气都作罢 提交于 2019-12-02 02:24:03

问题


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


来源:https://stackoverflow.com/questions/7142220/tablelayoutpanel-getcellposition-and-getpositionfromcontrol-how-do-they-differ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!