问题描述
前段时间我写了一个 UserControl。此控件包含 TP_PACKER
作为 TableLayoutPanel
和 Dock = Fill 并包含两行。 1 行是绝对变量值。它包含 RichTextBox
控件。我可以更改 RichTextBox
的大小,也可以调整 1 行的大小。第 2 行是百分比 = 100%。它包含一个 ListView
。
.Add(New RowStyle(SizeType.Absolute,TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize))
.Add(New RowStyle(SizeType.Percent,100))
在加载时一切正常,但在调整表单大小时,我的控件出现在顶部。从视觉上看,它看起来像 ListView
被放置在 TP_PACKER
的第一行,而不是第二行。
一开始我决定尝试以编程方式调整控件大小。我写了一段代码:
Private Sub ComboBoxExtended_Resize(sender As Object,e As EventArgs) Handles Me.Resize
UpdateSize()
End Sub
Protected Overrides Sub UpdateSize()
If _ControlInit AndAlso Not _SuspendControlResize Then
'If Not _TpSizeSet AndAlso Not _SuspendControlResize AndAlso _ControlInit AndAlso Not TP_PACKER Is nothing Then
' _SuspendControlResize = True
' TP_PACKER.RowStyles(0).Height = TextBoxHeight + MyBase.BorderStyleSize
' _TpSizeSet = True
'End If
_SuspendControlResize = True
Dim h%
Invalidate()
SuspendLayout()
If Not TP_PACKER Is nothing Then
With TP_PACKER
.Invalidate()
If Not .RowStyles(0).Height = TextBoxHeight + MyBase.BorderStyleSize Then
.RowStyles(0).Height = TextBoxHeight + MyBase.BorderStyleSize
.Refresh()
End If
End With
End If
If ListDropDownStyle = ListMode.Simple AndAlso Not ResultsList Is nothing Then
h = ResultListsHeight(ResultsList,True)
ResultsList.Invalidate()
TP_PACKER.Invalidate()
ResultsList.Size = New Size(ResultsList.Width,h)
TP_PACKER.Refresh()
ResultsList.Refresh()
h += (TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize * 3)
h += 1
Else
h = TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize
End If
'If Not Me Is nothing AndAlso Not Parent Is nothing Then
' Dim p = Parent.Height - Parent.Margin.Top - Parent.Margin.Bottom
' If h > p Then h = p
'End If
Try
Height = h
If _ControlInit Then Width = MinWidth
Catch ex As Exception
Finally
UpdateBounds()
ResumeLayout(True)
_SuspendControlResize = False
End Try
End If
End Sub
Private Function ResultListsHeight(ByRef l As ListView,ByVal IsMainList As Boolean) As Integer
Dim h% = 1
If Not l Is nothing Then h = Math.Max(l.Items.Count,1)
h = Math.Min(ListMaxDropDownItems,h)
If IsMainList AndAlso ListDropDownStyle = ListMode.Simple Then
If Dock = DockStyle.Fill Then
If Not TP_PACKER Is nothing Then
With TP_PACKER
Dim AdditHeight% = TextBoxHeight + MyBase.BorderStyleSize +
.Margin.Top + .Margin.Bottom +
.Padding.Top + .Padding.Bottom + BorderStyleSize * 2 '(BorderStyleSize * 3)
h = .Height - AdditHeight + ListColumnsHeigh() '- BorderStyleSize - 3
If Not Parent Is nothing Then h -= Parent.Padding.Bottom
End With
End If
Else
h = (ListMaxDropDownItems * ListRowHeigh + ListColumnsHeigh())
End If
Else
h *= ListRowHeigh
h = h + ListColumnsHeigh() + BorderStyleSize
End If
Return h
End Function
ListView
使用参数创建:
ResultsList= New ListView With {
.View = View.Details,.BorderStyle = BorderStyle.FixedSingle,.MultiSelect = False,.FullRowSelect = True,.Hideselection = False,.HeaderStyle = IIf(ListColumnsHeadersVisible,ColumnHeaderStyle.Nonclickable,ColumnHeaderStyle.None),.GridLines = ListGridVisible,.Dock = DockStyle.Fill,.Margin = New Padding(0)
}
此函数调整控件大小以适应父级。它有部分帮助。在我将控件放入 GroupBox
的地方,我的控件可以正常工作并正确调整大小。但是在我将控件放入 TableLayoutPanel
的其他表单中,它没有正确调整大小。例如:
正确:
不正确:
我不明白为什么会这样。我尝试将 Anchor
更改为 Left+Top 和 Left+Top+Right+Bottom。什么都没有改变。
更新:
在调试我的控件中的每个元素并查看这些属性后,我注意到在调整大小后控件的边界被删除了。所以:
1-st:ListView 应该停靠为 DockStyle.Top
(而不是 DockStyle.Fill
);
2-nd:我在更新 ResultsList.SetBounds(1,TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize,ResultsList.Size.Width,ResultsList.Size.Height)
现在一切正常。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)