首页 > 解决方案 > 用户控件调整大小

问题描述

前段时间我写了一个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)

现在一切正常。

标签: vb.netuser-controlsresizewindow-resize

解决方案


推荐阅读