Results 1 to 9 of 9

Thread: Make a text box grow to accomodate input [Resolved]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476

    Make a text box grow to accomodate input [Resolved]

    Hi team

    I want a text box to grow dynamically in height as the user enters text. I have this working by counting the number of characters and dividing this by the typical number of characters that fit onto a line. However, some characters take up less space that others. 50 "i"s will cause it to create an new line when it does not need one, but 50 "W"s does not create a new line when one is needed. I want the user to be able to continue typing and have the text box elegantly grow to accomodate all the entered text.

    One idea is to intercept the events that tell a scroll bar (if one existed, I don't want one) how much text there is and how long the scroll bar needs to be. I could then use these values to set the height. Does anyone know how to do this?

    Has anyone got any other ideas?

    Thanks FW
    Last edited by freewilly; Nov 24th, 2003 at 05:17 PM.

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    G'Day FreeWilly,


    A while back there was a question about wrapping text (I think to the printer). The solution
    may be something you could adapt/re-code to your requirements.

    I think it was Hack, Peet or plenderj that came up with a suitable solution.




    Bruce.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Use Me.Textwidth

    VB Code:
    1. Text1.Width = Me.TextWidth(Text1.Text)

  4. #4
    Lively Member
    Join Date
    Mar 2003
    Posts
    68
    Have you considered a RichTextBox?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    Thanks Guys, I think I will be able to use TextWidth.

    Bruce, I'll look for that thread if I fail with TextWidth

    Thanks

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Here tis for what its worth (It was BeachBum )


    Bruce.

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    Nicwe one Froggy ,

    I found that this seems to work well:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Form_Load()
    4.     [b]Me.ScaleMode = vbTwips[/b]
    5. End Sub
    6. Private Sub Command1_Click()
    7.     Text1.Width = Me.TextWidth(Text1.Text) [b]+ 100[/b]
    8. End Sub



    Bruce.

  8. #8
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    You probably shouldn't use TextWidth to change the height of the textbox but rather the TextHeight. To use TextHeight you need to know how many lines there are. Here's how to do the whole thing.

    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" _
    3.                         (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _
    4.                         As Long, ByVal lParam As Long) As Long
    5. Const EM_GETLINECOUNT = 186
    6.  
    7. Private Sub Text1_Change()
    8.  
    9.   Dim lngLineCount As Long
    10.  
    11.   ' Make sure we're at the beginning
    12.   Text1.SelStart = 0
    13.   ' Count the number of lines in Text1
    14.   lngLineCount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
    15.   ' Multiply line count by line height and add 120 for border
    16.   Text1.Height = TextHeight(Text1.Text) * lngLineCount + 120
    17.   ' Make sure the next character gets placed at the end
    18.   Text1.SelStart = Len(Text1.Text) + 1
    19. End Sub

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2000
    Location
    Sydney Australia
    Posts
    476
    Thanks guys

    Marty yours works a treat, thanks

    FW

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