making a form with a textbox that resizes to fit content

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

Gidmaestro

Guest
I have a replacement for the standard VB messageBox. My replacement is a form with a textbox and an OK button in it. If I want to show this custom messageBox, I call it like this:


dim msg as string = "four score and twenty years ago, our fathers...."
if vbOK = MyMessageBox(msg) then
' do something
End If


The problem is that even though it resizes the textbox to the correct height, the text still stays
all on one line, its just that you have to select all of it to see the part that extends beyond the visible right side of the textbox.In other words, if you set the message as above, you might see a textbox with enough space for two vertical lines, but all the text is on the first line. It doesn't wrap.


Function MyMessageBox sets the text in the textbox, and then shows the form:

Public Shared Function MyMessageBox(ByVal message As String) As DialogResult
Dim fm As New FormMessageBox
Dim dr As DialogResult
fm.TextBox1.Text = message
dr = fm.ShowDialog()

Return dr
End Function


The actual form with the textbox has a form_shown event that tries to resize the textbox.


Private Sub FormMessageBox_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Dim textLength As Integer
Dim numlines As Integer
Dim margin As Integer = 0
Dim boxbottom As Integer
Dim btntop As Integer
textLength = TextBox1.Text.Length
numlines = TextBox1.GetLineFromCharIndex(textLength) + 1
TextBox1.Height = TextBox1.PreferredHeight * numlines + margin
boxbottom = TextBox1.Top + TextBox1.Height
btntop = boxbottom + 12
ButtonOK.Top = btntop
ButtonOpen.Top = btntop
ButtonEmail.Top = btntop
ButtonViewAndEmail.Top = btntop
Me.Height = boxbottom + 100
TextBox1.WordWrap = True
TextBox1.Refresh()
Me.TextBox1.SelectionLength = 0
End Sub

Any help is appreciated.

Continue reading...
 
Back
Top