Results 1 to 4 of 4

Thread: TextBox width, height depends on string lenght

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2000
    Location
    Europe, Lithuania
    Posts
    309

    TextBox width, height depends on string lenght

    Hey,

    I'm creating textbox'es dynamically (in ASP .NET) from values, which I get from DB.
    I get value which is maximum allowed string lenght.

    How to resize textbox to size, when width maximum value is specified.

    Etc.:

    Maximum string lenght can be 500 chars.
    I do not want long singleline texbox.
    It should have properties multiline = true, height = [textheight * lines count] when it reachs maximum width.

    Thanks in advance

  2. #2
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    I'm not sure that you will get what you want unless you are using a fixed size font like Courier New.

    Most fonts are variable length, so a W takes up more width than an I. I guess you could go through and figure out how many pixels each letter, upper and lower case, use then increment through the string and come up with a total width needed, but why? If you set the multiline to true and the data is to long, VB.NET will automatically wrap the text and add a scroll bar. This way, all of your textboxes will be the same width (shorter data will not have a shorter textbox) which I think looks better.

    Or did I completely miss the point?

  3. #3
    Member
    Join Date
    Feb 2004
    Location
    LA
    Posts
    57
    Hello,

    Her is the code on how to resize the text box depending on the text length



    Code:
    Private Sub txttest_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttest.TextChanged
    
     txttest.Size = New System.Drawing.Size(txttest.Width + txttest.TextLength, txttest.Height)
    
        End Sub

  4. #4
    Lively Member
    Join Date
    Nov 2003
    Location
    Chicagoland
    Posts
    92
    In your code, the txttest.Size is expecting two parameters, the width and the height, in pixels. When you do the txttest.width and txttest.height you are grabbing the width and height of the textbox in pixels. So far so good.

    However, you are using the TextLength property incorrectly. That returns the number of characters in a string, not how many pixels it takes up on the screen. So when you add it to the width, you are not actually adding on that much.

    For instance, I have a label that is (32,16) meaning 32 pixels wide and 16 pixels tall. That is the smallest I can get it to fit the text "Date". If I wanted to change the text to "Date/Time", I need (56,16). Using your code, txttest.Width would be 32, txttest.TextLength would be 9. Therefore I would only get a textbox that is (41,16), not nearly long enough.

    This would be completely different if I used a different font.

    In order to do what you want, you have to figure out what font you want to use. Then you have to figure out how many pixels you need for each letter. Then you have to something like declaring a new variable "newWidth" and starting it at 0. Then loop through your text string, determining what the character is, and adding the number of pixels required for that letter to "newWidth". then you set your size to (newWidth, txttest.Height).

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