TableLayoutPanel插入问题 [英] TableLayoutPanel Insert Issue

查看:70
本文介绍了TableLayoutPanel插入问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Form_load上,三个标签动态插入TableLayoutPanel。单击第一个标签,弹出两个标签作为子菜单。现在我的isuue是,在点击第二个标签时,其子菜单标签出现在第一个标签下而不是第二个标签下。





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

结束结构

结束类

On Form_load three labels are inserted dynamically to the TableLayoutPanel.On clicking the first label, two labels popup as submenus. Now my isuue is, on clicking the second label, its submenu labels are appearing under the first label instead of second label.


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

推荐答案

找到解决方案。只需添加



found the solution.just add

'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插入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆