I cannot adjust the height of my form programmatically to appropriately fit its contents.

  • Thread starter Thread starter Gidmaestro
  • Start date Start date
G

Gidmaestro

Guest
I have a form with a text box that can have different text at different times. The textbox shrinks to fit the text (I have code to do that), but the form doesn't adjust properly to fit its own contents. It becomes too small (not high enough).


The form itself has a little picture box at the top (for an optional icon) and below that comes a textbox, and below the textbox comes an OK button.

Here is my code, any help is appreciated:

Private Sub FormMessage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToParent()
If PictureBoxForIcon.Image Is Nothing Then
Dim oldtop As Integer

oldtop = TextBoxMessage.Top
PictureBoxForIcon.Visible = False
TextBoxMessage.Top = 2
TextBoxMessage.Height = TextBoxMessage.Height + oldtop - 2
End If
ShrinkBoxToFit(TextBoxMessage)
Dim bottomTextBox As Integer
Dim bottomButton As Integer

bottomTextBox = TextBoxMessage.Top + TextBoxMessage.Height
ButtonSubmit.Top = bottomTextBox + 4
bottomButton = ButtonSubmit.Top + ButtonSubmit.Height
Me.Height = bottomButton + 7
End Sub
Private Sub ShrinkBoxToFit(ByRef sender As TextBox)
Dim tempFont As Font = sender.Font
Dim textLength As Integer = sender.Text.Length
Dim textLines As Integer = sender.GetLineFromCharIndex(textLength) + 1
Dim Margin As Integer = sender.Bounds.Height - sender.ClientSize.Height
sender.Height = (TextRenderer.MeasureText(" ", tempFont).Height * textLines) + Margin + 2
End Sub


I should say that the way I create this form is in a routine as follow:

Public Shared Function ErrorBox(ByVal Message As String, Optional ByVal tellAboutCaller As Boolean = False) As DialogResult

Dim fm As New FormMessage
fm.TextBoxMessage.Text = Message
fm.Text = "Error Message"
fm.ButtonSubmit.Text = "OK"
fm.PictureBoxForIcon.Image = My.Resources.ResourceManager.GetObject("errorSymbol")
fm.ShowDialog()
Return DialogResult.OK
End Function

Continue reading...
 
Back
Top