I have a textbox, whose height I would dynamically changed based on the content: the longer the text and more lines it wraps, the taller I would like it to be. However, when .AutoSize and .MultiLine are both true, the textbox does not increase in height, but instead displays scrollbars. Perhaps this is the intention. If so, how can I dynamically calculate the height needed to display all text, taking into consideration the device height and width?
thanks
Andrew
Unfortunately when there are no line-breaks present, textBox.Lines.Length is always 1, even when lines are wrapped. A good solution should account for a single piece of long-wrapped text, and also multi-line comments that have breaks in them (vbBreak).
To handle this I also used MeasureText to calculate the number of lines required to render a long string that is absent of breaks, and will be wrapped. Then we use the larger of the 2 values. It seems to work — thank you for the nudge in the right direction:
Public Sub AdjustDescriptionHeight()
Const padding As Integer = 3
Dim numLines As Integer = Me.txtDescription.Lines.Length
‘calculate lines in case of one-long piece of text, considering textbox width
Dim s As Size = Wisej.Base.TextUtils.MeasureText(txtDescription.Text, txtDescription.Font)
Dim intLinesRequiredToRender = Math.Ceiling(s.Width / txtDescription.Width)
numLines = Math.Max(numLines, intLinesRequiredToRender) ‘use whatever value is larger.
Dim border As Integer = 1
txtDescription.Height = txtDescription.Font.Height * numLines + padding + border
End Sub
Hi Andrew,
you could follow the approach that David Sherret suggess here:
https://stackoverflow.com/questions/2893059/autoresize-textbox-control-vertically
instead of GetLineFromCharIndex you can use this.txtBody.Lines.Length
You might want to fine tune the formula a bit also depending on the device.
Hope that helps.
Best regards
Frank
Please login first to submit.