Some time ago I wrote a UsserControl. This control contains TP_PACKER
as TableLayoutPanel
with Dock = Fill and contains two rows. 1 row is absolute with variable value. It contains RichTextBox
control. I can change size of RichTextBox
and I resize 1 row also. 2 row is percentage = 100%. It contain a ListView
.
.Add(New RowStyle(SizeType.Absolute, TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize))
.Add(New RowStyle(SizeType.Percent, 100))
On loading all is OK but on form resize my control was gone to top. Visually its look like ListView
is placed in 1-st row of TP_PACKER
, not 2-nd.
At start I decided to try resize control programmatically. I wrote a code:
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
Created with parameters:
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)
}
This function resize control to fit in parent. And it helps partially. In where I placed control into GroupBox
my control is working and resizing correctly. But on other form where I place control into TableLayoutPanel
it is not resize correctly. For example:
Correctly:
NOT Correctly:
I do not understand why is it happens. I tried to change Anchor
to Left+Top and Left+Top+Right+Bottom. Nothing changed.
UPDATE:
After debug each element in my control and review these properties, control's bounds was dropped after resize, I noticed. So:
1-st: ListView should be docked as DockStyle.Top
(not DockStyle.Fill
);
2-nd: I set ListView bounds again on updating by ResultsList.SetBounds(1, TextBoxHeight + MyBase.BorderStyleSize + BorderStyleSize, ResultsList.Size.Width, ResultsList.Size.Height)
Now all is OK.