vb.net - 用户控件调整大小
问题描述
前段时间我写了一个UsserControl。该控件与 Dock = FillTP_PACKER
一样TableLayoutPanel
包含两行。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
(not DockStyle.Fill
);
2-nd:我在更新时再次设置 ListView 边界ResultsList.SetBounds(1, TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize, ResultsList.Size.Width, ResultsList.Size.Height)
现在一切正常。
解决方案
推荐阅读
- python - 通过它们在字母表中出现的索引“排列”字符串中的每个字符
- java - For循环java将输出分为2列
- assembly - emu8086 中的“@”符号是什么?
- javascript - 当我想获取数据时出现此错误:TypeError: Cannot read property 'data' of undefined
- c# - 无法将“Org.BouncyCastle.Crypto.AsymmetricCipherKeyPair”类型的对象转换为“Org.BouncyCastle.Crypto.Parameters.RsaKeyParameters”类型的对象
- angular - Angular 10延迟加载路由不起作用
- python - 在 Python 中的 Pandas Dataframe 中按 JSON 对象中的值聚合
- c# - C# 泛型 - 管理每个类型加 1
- c# - 如何为 Visual Studio 测试资源管理器创建适配器?
- haskell - 获取haskell中字符串的所有旋转