问题描述
Public Class Form1
Private Sub Form1_Load (发送者为System.Object,e As System.EventArgs)处理MyBase.Load
Dim objLbl As Label
TableLayoutPanel1.RowStyles.Clear ()
TableLayoutPanel1.Controls.Clear()
TableLayoutPanel1.RowCount = 0
for i As Integer = 0 To 2
TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(SizeType.AutoSize,20))
TableLayoutPanel1.RowCount + = 1
objLbl =新标签
objLbl.Font = New Drawing.Font(Segoe UI,12,FontStyle.Regular)
objLbl.Text =Label:+ i.ToString ()
TableLayoutPanel1.Controls.Add(objLbl,0,i)
AddHandler objLbl.Click,AddressOf objLbl_Click
Next
End Sub
Private Sub objLbl_Click(ByVal sender As Object,ByVal e As System.EventArgs)
Dim objLbl As Label
Dim tempHolding As New List(Of HoldingCell)
Dim cell作为HoldingCell
Dim intPosition As Integer
intPosition = TableLayoutPanel1.GetRow(sender)
使用TableLayoutPanel1
对于行As Int32 = intPosition To TableLayoutPanel1.RowCount - 1
对于col as Int32 = 0 To TableLayoutPanel1.ColumnCount - 1
cell = New HoldingCell
cell.cntrl = .GetControlFromPosition(col,row)
''恢复的设置位置=当前行+ 1
cell.pos = New TableLayoutPanelCellPosition(col,row + 1)
tempHolding.Add(cell)
Next col
下一行
''插入新行
对于intRow = 0到1
.RowStyles.Insert(intPosition,New RowStyle (SizeType.Absolute,20.0!))
.RowCount + = 1
下一页
''调整控制位置
每个单元格在tempHolding
如果cell.cntrl IsNot Nothing那么
.SetCellPosition(cell.cntrl,cell.pos)
结束如果
下一个单元格
''为插入的行添加控件
对于intRow = 0到1
objLbl =新标签
objLbl.Text = Space(5)& 子标签& intRow
TableLayoutPanel1.Controls.Add(objLbl,0,intPosition)
下一页
结束
End Sub
结构HoldingCell
Dim cntrl作为控制
Dim pos As TableLayoutPanelCellPosition
结束结构
结束类
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim objLbl As Label
TableLayoutPanel1.RowStyles.Clear()
TableLayoutPanel1.Controls.Clear()
TableLayoutPanel1.RowCount = 0
For i As Integer = 0 To 2
TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(SizeType.AutoSize, 20))
TableLayoutPanel1.RowCount += 1
objLbl = New Label
objLbl.Font = New Drawing.Font("Segoe UI", 12, FontStyle.Regular)
objLbl.Text = "Label: " + i.ToString()
TableLayoutPanel1.Controls.Add(objLbl, 0, i)
AddHandler objLbl.Click, AddressOf objLbl_Click
Next
End Sub
Private Sub objLbl_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim objLbl As Label
Dim tempHolding As New List(Of HoldingCell)
Dim cell As HoldingCell
Dim intPosition As Integer
intPosition = TableLayoutPanel1.GetRow(sender)
With TableLayoutPanel1
For row As Int32 = intPosition To TableLayoutPanel1.RowCount - 1
For col As Int32 = 0 To TableLayoutPanel1.ColumnCount - 1
cell = New HoldingCell
cell.cntrl = .GetControlFromPosition(col, row)
''setup position for restore = current row + 1
cell.pos = New TableLayoutPanelCellPosition(col, row + 1)
tempHolding.Add(cell)
Next col
Next row
''insert new row
For intRow = 0 To 1
.RowStyles.Insert(intPosition, New RowStyle(SizeType.Absolute, 20.0!))
.RowCount += 1
Next
''adjust control positions
For Each cell In tempHolding
If cell.cntrl IsNot Nothing Then
.SetCellPosition(cell.cntrl, cell.pos)
End If
Next cell
''add controls to the inserted row
For intRow = 0 To 1
objLbl = New Label
objLbl.Text = Space(5) & "sub label " & intRow
TableLayoutPanel1.Controls.Add(objLbl, 0, intPosition)
Next
End With
End Sub
Structure HoldingCell
Dim cntrl As Control
Dim pos As TableLayoutPanelCellPosition
End Structure
End Class
推荐答案
'Shift exist controls down
For Each ExistControl As Control In TableLayoutPanel1.Controls
If TableLayoutPanel1.GetRow(ExistControl) >= intPosition Then
TableLayoutPanel1.SetRow(ExistControl, TableLayoutPanel1.GetRow(ExistControl) + 1)
End If
Next
下面
''调整控制位置
部分代码。
谢谢
below
''adjust control positions
part of the code.
Thanks
这篇关于TableLayoutPanel插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!