Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

??? Autosizing a TEXTBOX vertically?

Status
Not open for further replies.

thesleepylizard

Programmer
Mar 18, 2002
41
AU
Hi guys,

The VB Label control has an "autosize" property. If this is true, then the label will automatically resize itself horizontally to snugly fit the text it's given.

Now, I want to do a similar with with a Textbox. And, I want it to keep the WIDTH the same, but, resize itself vertically and spread the text over multiple lines. So I might set the text to "The quick brown fox jumps over the lazy dog", and it would format it as:


Code:
The quick brown 
fox jumps over
the lazy dog

And, most important, ensuring that it resizes itself vertically to display ALL the text, with on scrollbars required.

Any ideas how to do this? I started writing a routine based on analyising the string and using the .TextHeight and .TextWidth properties of the form to intelligent figure things out, and I'm happy to do this if I have to. I want to avoid using special .OCXs and stick with the standard VB controls.

Thanks for your time,
-lizard
 
Just set Textbox.multiline property to true and set Textbox.Scrollbars to 2 (vertical)
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 

<<<Just set Textbox.multiline property to true and set Textbox.Scrollbars to 2 (vertical)>>>

But, this doesn't actually resize the text box if the text is too big - it just enables the scroll bar.

Sorry, I said &quot;...to display ALL the text, with on scrollbars required.&quot; I might have caused confusion here, and I have no idea what I meant by that. Coffee...


In the mean time, I've figured out some neat code to do what I'm after:

Code:
For CharacterIndex = 1 To Len(pText)
    If Mid(pText, CharacterIndex, 1) = &quot; &quot; Then LastSpace = CharacterIndex
    If .TextWidth(Mid(pText, LastBreak + 1, CharacterIndex - LastBreak + 1)) >= .Text.Width Then
        If LastSpace > LastBreak Then CharacterIndex = LastSpace
        .Text = .Text + Mid(pText, LastBreak + 1, CharacterIndex - LastBreak) & vbCrLf
        LastBreak = CharacterIndex
    End If
Next CharacterIndex

Needs a bit of work but it's getting the job done. BTW you can make it wrap around a picture in the top right corner by saying this instead:

Code:
If .TextWidth(Mid(pText, LastBreak + 1, CharacterIndex - LastBreak + 1)) >= .Text.Width - (IIf(.Icon.Height > .TextHeight(.Text), .Icon.Width, 0)) Then

Cheers,
-lizard
 
Sorry, that block of code should be enclosed in:

Code:
With MyForm
.
.
end With
 
Today is NOT my day for writing superb code. The complete code should be something like:

Code:
With MyForm
For CharacterIndex = 1 To Len(pText)
    If Mid(pText, CharacterIndex, 1) = &quot; &quot; Then LastSpace = CharacterIndex
    If .TextWidth(Mid(pText, LastBreak + 1, CharacterIndex - LastBreak + 1)) >= .Text.Width Then
        If LastSpace > LastBreak Then CharacterIndex = LastSpace
        .Text = .Text + Mid(pText, LastBreak + 1, CharacterIndex - LastBreak) & vbCrLf
        LastBreak = CharacterIndex
    End If
Next CharacterIndex
.Text = .Text + Mid(pText, LastBreak + 1, CharacterIndex - LastBreak)
End With

Ok, if there are any errors in this I think I'll just shut up. Good thing we don't need this for a while lol...
 
[tt]
Option Explicit

Public Const EM_GETLINECOUNT = &HBA
Public Declare Function SendMessage Lib &quot;user32&quot; Alias &quot;SendMessageA&quot; (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long


' Assumes that parent form scalemode is Twips
' and only calculates client area height; i.e does not
' take any borders set on the textbox into consideration
Public Function CalcHeight(ctlEdit As TextBox) As Long
CalcHeight = SendMessage(ctlEdit.hWnd, EM_GETLINECOUNT, 0&, 0&) * ctlEdit.Parent.TextHeight(&quot;I&quot;)
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top