Apr 8th, 2014, 06:57 PM
Auto setting a textbox height property to fit text.
This is an FYI Only.
I spent a lot of time trying to get a asp textbox, that has it's multiline property set to true, to automatically set it's height property
It always seems to come up with a default value of 120px no matter how big the text inside the box is.
Anyway, to solve the problem I created a function as follows in a module;
Public Function SetTextHeight(n As Int32) As Int16
'n is the length of the string in the textbox
Return n \ 70 + 1
Now I set the width of the Textbox to a width of 70 columns. The 70 matches the 70 in the function.
if you set the column width to 50, change the 70 in the function to 50. Could also be sent as an argument.
Now I use the return value as the Rows property of the textbox. see the following snippet.
txt = New TextBox
txt.ReadOnly = True
txt.Font.Size = "16"
txt.Font.Bold = True
txt.ForeColor = Drawing.Color.DarkGreen
txt.BackColor = Drawing.Color.Moccasin
txt.Columns = 70 'This is where the column width is set.
txt.Wrap = True
txt.TextMode = TextBoxMode.MultiLine
txt.Text = r.Item("Message")
txt.Rows = SetTextHeight(Len(txt.Text))
Very simple and it seems to work rather nicely.
Last edited by peteshir; Apr 8th, 2014 at 07:05 PM.
Apr 8th, 2014, 08:59 PM
Re: Auto setting a textbox height property to fit text.
Thread moved to the CodeBank - ASP.Net forum.
If anyone's answer has helped you, please show your appreciation by rating that answer.
I'd rather run ScriptBrix...
Joacim's view on stuff
Click Here to Expand Forum to Full Width