Solved

Autosizing a TextBox/RichText Box

Posted on 2001-08-12
29
276 Views
Last Modified: 2012-06-21
How to autosize a textbox/rich Textbox control while typing so that the contents fit into the client area always without scroll bars
0
Comment
Question by:cvbala
  • 7
  • 5
  • 4
  • +6
29 Comments
 
LVL 12

Expert Comment

by:roverm
ID: 6377446
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
 
LVL 1

Expert Comment

by:DandL
ID: 6377459
Text1.Width = Text1.Width + 90
 Me.Width = Text1.Width
0
 
LVL 17

Expert Comment

by:smozgur
ID: 6377946
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
 
LVL 3

Expert Comment

by:rmichels
ID: 6377984
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
 
LVL 2

Expert Comment

by:JanusFury
ID: 6378365
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
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6378447
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
 
LVL 12

Expert Comment

by:roverm
ID: 6378785
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
 
LVL 17

Expert Comment

by:smozgur
ID: 6378875
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
 
LVL 12

Expert Comment

by:roverm
ID: 6378960
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
 
LVL 6

Expert Comment

by:VK
ID: 6380209
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
 
LVL 6

Expert Comment

by:VK
ID: 6380214
of course:

lblResize.visible=false

0
 
LVL 6

Expert Comment

by:VK
ID: 6380284
Example:

Private Sub SetCTRLWidth(CTRL As Control)
    Set lblResize.Font = CTRL.Font
    lblResize.Caption = CTRL.Text & "W"
    CTRL.Width = lblResize.Width
End Sub
0
 
LVL 22

Expert Comment

by:rspahitz
ID: 6381188
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
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6382547
Hi VK,

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

That's it!

glass cookie : )
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 6

Expert Comment

by:VK
ID: 6383625
Hi glass cookie !

You're right. Your comment was the first correct one :-)
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6383844
Hi VK,

Your's correct too : )

: )

That's it!

glass cookie : )
0
 
LVL 2

Expert Comment

by:JanusFury
ID: 6385083
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
 
LVL 12

Expert Comment

by:roverm
ID: 6385136
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
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6387047
Or... how about a point split (heeheehee) ; )
0
 
LVL 2

Expert Comment

by:JanusFury
ID: 6387054
point split! cool!
i just want a t-shirt :)
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6387085
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
 
LVL 22

Expert Comment

by:rspahitz
ID: 6389168
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
 
LVL 12

Expert Comment

by:roverm
ID: 6419733
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
 

Author Comment

by:cvbala
ID: 6426656
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
 
LVL 12

Expert Comment

by:roverm
ID: 6426728
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
 
LVL 3

Expert Comment

by:rmichels
ID: 6426930
cvbala

My comment directed you to that kb article.  Did you not see it?
0
 

Author Comment

by:cvbala
ID: 6427333
Dear rmichels,
Yes, I saw it that is how I found it. I also accepted your answer. Did you get the pointS?
0
 
LVL 3

Accepted Solution

by:
rmichels earned 200 total points
ID: 6427400
Thanks :) The question still appears to be open
0
 
LVL 12

Expert Comment

by:roverm
ID: 6427684
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

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB6 convert MSHFlexgrid1 cells 7 52
VB 6.0 printer how to align 6 49
VBA filters 2 39
vb6 connector to mongodb 2 27
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now