I'm creating a dialog box in VB.NET from certain elements. I have options to have a label, one or more buttons, a text box, and/or a combobox. I use the BuildForm() procedure to calculate the size of all the elements to fit correctly. Once that is done, I use Form.ShowDialod() to display it, but I've found that my controls are not retaining their sizes. Specifically a Label control that had been calculated to fit its text goes from 115px to 92px.
It is anchored to the form, but even without resizing the form at all, the control changes size. In this example, I can see that lblDsc.height changed from 115 to 92.
What can be causing this and how do I mitigate it? I thought calculating the controls beforehand would be straightforward.
Public Function MyInputButtonBox(ByVal description As String, title As String, ParamArray buttons As String()) As Integer
If buttons.Length = 0 Then Return -1
inpBox = Nothing
BuildForm(description, buttons)
inpForm.Text = title
MsgBox(lblDsc.Height)
inpForm.ShowDialog()
MsgBox(lblDsc.Height)
Return inpForm.Tag
inpForm.Dispose()
End Function
For more context, here is the portion of BuildForm that calculates lblDsc's attributes. Note that AutoSize is set to False and I moved the anchoring code to the end after the form size had been finalized.
'Description
lblDsc.AutoSize = False
lblDsc.Top = 5
lblDsc.Left = 5
lblDsc.Width = btnPanel.Left - lblDsc.Left - 5
lblDsc.Text = description
Dim textSize = TextRenderer.MeasureText(lblDsc.Text, New Font(lblDsc.Font.FontFamily, lblDsc.Font.Size, lblDsc.Font.Style), lblDsc.Size, TextFormatFlags.WordBreak)
lblDsc.Height = Math.Ceiling(1.1 * textSize.Height)
lblDsc.Parent = inpForm
lblDsc.Visible = True
'lblDsc.Anchor = AnchorStyles.Left + AnchorStyles.Top + AnchorStyles.Right + AnchorStyles.Bottom
lblDsc.BorderStyle = BorderStyle.FixedSingle 'BorderStyle.None 'TODO restore no border after testing