Results 1 to 2 of 2

Thread: Auto setting a textbox height property to fit text.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2008
    Posts
    21

    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
    End Function

    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))
    pnlMess.Controls.Add(txt)



    Very simple and it seems to work rather nicely.
    Last edited by peteshir; Apr 8th, 2014 at 07:05 PM.

  2. #2
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Auto setting a textbox height property to fit text.

    Thread moved to the CodeBank - ASP.Net forum.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width