检索放置添加到 TableLayoutPanel 的控件的行
Retrieve row where control added to a TableLayoutPanel was placed
我有一个向 TableLayoutPanel
添加控件的操作,我想将每个新行的 SizeType
设置为 AutoSize
。
下面的代码不起作用,因为它没有检索实际的行号,.GetRow(e.Control)
的结果变为 -1
。我应该处理哪个事件以获得正确的索引?
Private Sub TableLayoutPanel1_ControlAdded(sender As Object, e As ControlEventArgs) Handles TableLayoutPanel1.ControlAdded
Dim i = TableLayoutPanel1.GetRow(e.Control) 'i = -1 ... why?
TableLayoutPanel1.RowStyles(i).SizeType = SizeType.AutoSize
End Sub
您必须read/intuit非常仔细地阅读文档。
来自TableLayoutPanel.GetRow Method
The row position of control, or -1 if the position of control is determined by LayoutEngine.
该语句的 LayoutEngine 部分是关键。您需要一种方法来 return 由它确定的位置。幸运的是你有 TableLayoutPanel.GetPositionFromControl Method.
The GetPositionFromControl method returns the actual current position of control, even if its position is determined by the LayoutEngine.
您可以继续使用 ControlAdded 事件。
Dim i As Int32 = TableLayoutPanel1.GetPositionFromControl(e.Control).Row
我有一个向 TableLayoutPanel
添加控件的操作,我想将每个新行的 SizeType
设置为 AutoSize
。
下面的代码不起作用,因为它没有检索实际的行号,.GetRow(e.Control)
的结果变为 -1
。我应该处理哪个事件以获得正确的索引?
Private Sub TableLayoutPanel1_ControlAdded(sender As Object, e As ControlEventArgs) Handles TableLayoutPanel1.ControlAdded
Dim i = TableLayoutPanel1.GetRow(e.Control) 'i = -1 ... why?
TableLayoutPanel1.RowStyles(i).SizeType = SizeType.AutoSize
End Sub
您必须read/intuit非常仔细地阅读文档。
来自TableLayoutPanel.GetRow Method
The row position of control, or -1 if the position of control is determined by LayoutEngine.
该语句的 LayoutEngine 部分是关键。您需要一种方法来 return 由它确定的位置。幸运的是你有 TableLayoutPanel.GetPositionFromControl Method.
The GetPositionFromControl method returns the actual current position of control, even if its position is determined by the LayoutEngine.
您可以继续使用 ControlAdded 事件。
Dim i As Int32 = TableLayoutPanel1.GetPositionFromControl(e.Control).Row