|
-
Nov 23rd, 2003, 09:58 PM
#1
Thread Starter
Hyperactive Member
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.
-
Nov 23rd, 2003, 10:59 PM
#2
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.
-
Nov 23rd, 2003, 11:01 PM
#3
Use Me.Textwidth
VB Code:
Text1.Width = Me.TextWidth(Text1.Text)
-
Nov 23rd, 2003, 11:11 PM
#4
Lively Member
Have you considered a RichTextBox?
-
Nov 23rd, 2003, 11:37 PM
#5
Thread Starter
Hyperactive Member
Thanks Guys, I think I will be able to use TextWidth.
Bruce, I'll look for that thread if I fail with TextWidth
Thanks
-
Nov 23rd, 2003, 11:51 PM
#6
Here tis for what its worth (It was BeachBum )
Bruce.
-
Nov 24th, 2003, 12:15 AM
#7
Nicwe one Froggy ,
I found that this seems to work well:
VB Code:
Option Explicit
Private Sub Form_Load()
[b]Me.ScaleMode = vbTwips[/b]
End Sub
Private Sub Command1_Click()
Text1.Width = Me.TextWidth(Text1.Text) [b]+ 100[/b]
End Sub
Bruce.
-
Nov 24th, 2003, 12:53 AM
#8
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:
Option Explicit
Private Declare Function SendMessageAsLong Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam _
As Long, ByVal lParam As Long) As Long
Const EM_GETLINECOUNT = 186
Private Sub Text1_Change()
Dim lngLineCount As Long
' Make sure we're at the beginning
Text1.SelStart = 0
' Count the number of lines in Text1
lngLineCount = SendMessageAsLong(Text1.hWnd, EM_GETLINECOUNT, 0, 0)
' Multiply line count by line height and add 120 for border
Text1.Height = TextHeight(Text1.Text) * lngLineCount + 120
' Make sure the next character gets placed at the end
Text1.SelStart = Len(Text1.Text) + 1
End Sub
-
Nov 24th, 2003, 05:16 PM
#9
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|