• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Autosizing a TextBox/RichText Box

How to autosize a textbox/rich Textbox control while typing so that the contents fit into the client area always without scroll bars
0
cvbala
Asked:
cvbala
  • 7
  • 5
  • 4
  • +6
1 Solution
 
rovermCommented:
This should do the trick:

Private Sub Text1_Change()
    If Text1.Width < TextWidth(Text1.Text + "AA") Then
        Text1.Width = TextWidth(Text1.Text + "AA")
    End If
End Sub

Just added the "AA" to the text to get some space after what you type.

Of course you have to check if the textbox doesn't go out the form ;-)

D'Mzzl!
RoverM
0
 
DandLCommented:
Text1.Width = Text1.Width + 90
 Me.Width = Text1.Width
0
 
Suat OzgurWeb / Application DeveloperCommented:
roverm; i think your nice code doesnot need "if". So it can be smaller to fit text also.

'just
Private Sub Text1_Change()
   Text1.Width = TextWidth(Text1.Text + "AA")
End Sub
'is ok.


Regards
suat
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
rmichelsCommented:
I don't know how to do it for a text box, but for a rich edit box check out the following links:


http://support.microsoft.com/support/kb/articles/Q257/8/49.ASP

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/hh/winui/richedit_3243.asp

If you cannot get to these links from here, goto www.microsoft.com and search on bottomless rich

I have used the code example show in the first link in my apps.

0
 
JanusFuryCommented:
These functions allow you to get the width and height of any string in pixels, in any arbitrary font. Simply add 8 pixels to width, and 4 to height, and you've got an autosizer. Enjoy :)

' Text Width and Height from arbitrary font
' by Janus (janusfury@citlink.net)

Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hDC As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As Size) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Type Size
        cX As Long
        cY As Long
End Type

Function TextWidthEx(Font As StdFont, Text As String) As Long
On Error Resume Next
Dim mMemoryDC As Long, mDesktopDC As Long, mOldFont As Long
Dim mSize As Size, mText As String, mTextLen As Long, mFont As IFont
    mDesktopDC = GetDC(GetDesktopWindow)
    mMemoryDC = CreateCompatibleDC(mDesktopDC)
    ReleaseDC GetDesktopWindow, mDesktopDC
    mOldFont = SelectObject(mMemoryDC, mFont.hFont)
    mText = Text
    mTextLen = Len(mText)
    Call GetTextExtentPoint32(mMemoryDC, mText, mTextLen, mSize)
    TextWidthEx = mSize.cX
    SelectObject mMemoryDC, mOldFont
    DeleteDC mMemoryDC
End Function

Function TextHeightEx(Font As StdFont, Text As String) As Long
On Error Resume Next
Dim mMemoryDC As Long, mDesktopDC As Long, mOldFont As Long
Dim mSize As Size, mText As String, mTextLen As Long, mFont As IFont
    mDesktopDC = GetDC(GetDesktopWindow)
    mMemoryDC = CreateCompatibleDC(mDesktopDC)
    ReleaseDC GetDesktopWindow, mDesktopDC
    mOldFont = SelectObject(mMemoryDC, mFont.hFont)
    mText = Text
    mTextLen = Len(mText)
    Call GetTextExtentPoint32(mMemoryDC, mText, mTextLen, mSize)
    TextHeightEx = mSize.cY
    SelectObject mMemoryDC, mOldFont
    DeleteDC mMemoryDC
End Function
0
 
glass_cookieCommented:
Hi!

How about doing this:

Create a label with it's zutosize property set to true.

Then in your textbox's change event,

Private Sub Text1_Change()
Label1.Caption = Text1.Text
Text1.Width = Label1.Caption + 40 'or whatever values you prefer
End Sub

That's it!

glass cookie : )
0
 
rovermCommented:
Hi suat:

The reason I first check the length is because then the textbox only resizes when it's predefined length will be exceeded.

D'Mzzl!
RoverM

0
 
Suat OzgurWeb / Application DeveloperCommented:
I see, i just meant that is good idea to make it smaller when text doesnot exceed but i see now it is not necessary here, because as you said above, "textbox only resizes when it's predefined length will be exceeded.".

i agree with roverm. And glass cookie has also same solution by different way i liked often. i often use other co-control to manage main controls by using co-control's properties which main control has not. (Haha, this is really bad English, sorry)


suat
0
 
rovermCommented:
The problem with adding a fixed length is that it will go wrong eventually. Too large or too small.
Then it's also depending on the textsize/type.

But you are right, glass_cookie's code will work also.

;-)

D'Mzzl!
RoverM
0
 
VKCommented:
Hello, i solved it in a very simple manner:

1. Place a label "lblResize" on your form
2. Set lblResize.AutoSize=True and the same font as the control you wish to resize.
3. If you type in the TextBox, first write the string in the label's caption
4. control.width=lblresize.width

:-)
0
 
VKCommented:
of course:

lblResize.visible=false

0
 
VKCommented:
Example:

Private Sub SetCTRLWidth(CTRL As Control)
    Set lblResize.Font = CTRL.Font
    lblResize.Caption = CTRL.Text & "W"
    CTRL.Width = lblResize.Width
End Sub
0
 
rspahitzCommented:
I've seen some applications that let you "zoom" on the data by opening a new form with only a textbox and OK/Cancel.  When done typing, the text is transferred back to the original box, afetr which you can easily resize without a lot of the concerns of autosize, and can even make it multiline as needed (if it exceeds the width of the window.)
0
 
glass_cookieCommented:
Hi VK,

We used around the same method with a different approach : )

That's it!

glass cookie : )
0
 
VKCommented:
Hi glass cookie !

You're right. Your comment was the first correct one :-)
0
 
glass_cookieCommented:
Hi VK,

Your's correct too : )

: )

That's it!

glass cookie : )
0
 
JanusFuryCommented:
Um, mine works too. And without any extra controls, just a font object... So mine was the first correct one, and then glass_cookie's ;P I'd suggest giving him the points, since using an autosize label is a pretty cheap and simple hack (no offense, all the other ways are more complicated) and the question-asker is most likely to use it.

Janus
0
 
rovermCommented:
Hi JanusFury!
>>Um, mine work too.
Yes, you are probably right! I think all solutions will work.
>>using an autosize label is a pretty cheap and simple hack
Yes, but you have to place an extra control on the form which comsumes memory, so easy is relative...
>>...and the question-asker is most likely to use it.
That's up to cvbala.

What I'm trying to say is that cvbala will pick her/his own solution from the comments. I didn't know we had to advertise for ourselves ;-)

D'Mzzl!
RoverM

Ps:
cvbala, are you still around ?
0
 
glass_cookieCommented:
Or... how about a point split (heeheehee) ; )
0
 
JanusFuryCommented:
point split! cool!
i just want a t-shirt :)
0
 
glass_cookieCommented:
I know it's out-of-point, but...

I've sent a total of about 5 posts to EE, 2 posts to Singapore customs, had hopes about the t-shirt, had notifications about getting the t-shirt ever since I had about 5000 points.  Now... I've yet to recieve even 1 shirt : |  Still waiting...

Is the t-shirt thingy back again?  I heard it was suspended some time ago due to some logistics problems.

That's it!

glass cookie : )
0
 
rspahitzCommented:
T-shirts suspended indefinitely. :(
EE is looking for a solid revenue source before it starts offering things like that again.

See the link in the "What's New" section.
0
 
rovermCommented:
cvbala:
Is your problem solved ? If so, please close the question by grading it.
If not, how can we help you any further ?

D'Mzzl!
RoverM
0
 
cvbalaAuthor Commented:
Well folks I found the answer at MS. See article Q257849 about bottomless text box. u have to subclass the parent window to get the job done neatly
Thanks all u
0
 
rovermCommented:
Hi cvbala!

If your question is solved then can you please close the question by grading it or PAQ-ing it.

If you think that an expert helped you in the right direction that you could give him/her the points.

You can also PAQ the question and let your question points be returned.
Just post a zero point question in CS (http://www.experts-exchange.com/jsp/qAskQuestion.jsp?ta=commspt) refering to this one and ask for PAQ-ing it.
Please don't delete it since there is some good information in it.

D'Mzzl!
RoverM
0
 
rmichelsCommented:
cvbala

My comment directed you to that kb article.  Did you not see it?
0
 
cvbalaAuthor Commented:
Dear rmichels,
Yes, I saw it that is how I found it. I also accepted your answer. Did you get the pointS?
0
 
rmichelsCommented:
Thanks :) The question still appears to be open
0
 
rovermCommented:
cvbala:
Did you accept rmichels comments as answer ? If not, please do so that the question will be closed.
If so, please leave a comment at CS (http://www.experts-exchange.com/jsp/qAskQuestion.jsp?ta=commspt) and ask for the question to be closed.

D'Mzzl!
RoverM
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 7
  • 5
  • 4
  • +6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now