×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Expand text box width to fit text

Expand text box width to fit text

Expand text box width to fit text

(OP)
Hello, I have a bound textbox named txtItemText and I need to have this textbox expand to the right to fit the text that's displayed. Is this possible and if so how?

RE: Expand text box width to fit text

Several ways of doing this. Do you want an API solution or a pure Access VBA solution?

RE: Expand text box width to fit text

If you just want to 'display' the text, I would use a label instead.
But if you do want to use a text box, are you going to allow changing the text / typing a new text in it?
If so, what about if you want to display 'ABC' in this text box, but you want to replace the 'ABC' with a lot longer text? Do you want to expand the text box while you type?


---- Andy

There is a great need for a sarcasm font.

RE: Expand text box width to fit text

(OP)
Its a bound textbox and when the record changes the text changes in the textbox. I wanted the width to fit the textboxs text each time it changed...

RE: Expand text box width to fit text

You did not answer any question... sad


---- Andy

There is a great need for a sarcasm font.

RE: Expand text box width to fit text

(OP)
Turn that frown upside down....Here is some answers smile

Quote (Andrzejek)

If you just want to 'display' the text, I would use a label instead.
I cant because its a bound textbox...

Quote (Andrzejek)

But if you do want to use a text box, are you going to allow changing the text / typing a new text in it?
I have to use a text box because its bound and I don't believe you can show data from a table in a label. I will not be typing data into this text box it only displays data that has previously been entered elsewhere.

Quote (Andrzejek)

If so, what about if you want to display 'ABC' in this text box, but you want to replace the 'ABC' with a lot longer text? Do you want to expand the text box while you type?
I'm not typing into the textbox its going to display data already entered elsewhere so as the records change new data will be displayed and the text box will have to be expanded each time.

@stromgm, I would rather use a pure Access VBA solution


Hope this helps.... Thanks!


RE: Expand text box width to fit text

In that case - and assuming you really want to do this (there are reasons why MS removed an autoSize capability from bound textboxes):

CODE

Private Sub Text0_Change()
     Text0.Width = WidthFromFontWiz(Text0.Text, Text0.FontName, Text0.FontSize)

    ' We need to add the padding
    Text0.Width = Text0.Width + Text0.LeftPadding + Text0.RightPadding
End Sub

Public Function WidthFromFontWiz(ByVal Caption As String, ByVal FontName As String, ByVal Size As Long, Optional ByVal Weight As Long = 400, Optional Italic As Boolean = False, Optional Underline As Boolean = False, Optional Cch As Long = 0, Optional MaxWidthCch As Long = 0) As Double
    Dim dx As Long
    Dim dy As Long
    
    ' Activate Access WizHook object 
    WizHook.Key = 51488399
    
    WizHook.TwipsFromFont FontName, Size, Weight, Italic, Underline, Cch, Caption, MaxWidthCch, dx, dy
    WidthFromFontWiz = dx + 15 '1 pixel short ...

End Function 

RE: Expand text box width to fit text

(OP)
Strongm, that works great thank you but one issue. The textbox doesn't extend long enough to show the full text, it cuts off half of the last letter. How do I extend it wider?

RE: Expand text box width to fit text

That'll almost certainly be because you have a border around the textbox which hasn't been calculated into the new width (my example only allowed for the internal padding in the control). So you should just need to change

Text0.Width = Text0.Width + Text0.LeftPadding + Text0.RightPadding

to

Text0.Width = Text0.Width + Text0.LeftPadding + Text0.RightPadding + Text0.BorderWidth * 2 * 15

RE: Expand text box width to fit text

(OP)
Strongm, I added it and its still not showing the full text, its cutting off half of the last word. I search the web for WizHook because I never heard of that and first thing was the code below so I tried it and It worked but I changed the 40 to 130 because it cut off the last bit also... Thanks!

Link I Found

CODE --> VBA

Public Sub AutoFit(ctl As Control)
    Dim lngWidth As Long
    lngWidth = GetTextLength(ctl, ctl.Value)
    ctl.Width = lngWidth + 130 '40
End Sub

Public Function GetTextLength(pCtrl As Control, ByVal str As String, _
        Optional ByVal Height As Boolean = False)
    Dim lx As Long, ly As Long
    ' Initialize WizHook
    WizHook.Key = 51488399
    ' Populate the variables lx and ly with the width and height of the
    ' string in twips, according to the font settings of the control
    WizHook.TwipsFromFont pCtrl.FontName, pCtrl.FontSize, pCtrl.FontWeight, _
                          pCtrl.FontItalic, pCtrl.FontUnderline, 0, _
                          str, 0, lx, ly
    If Not Height Then
        GetTextLength = lx
    Else
        GetTextLength = ly
    End If
End Function 

RE: Expand text box width to fit text

All they are doing is using a magic number instead of being explicit which my code is.

For example, with default padding (30 twips) and a border width of 2 pixels

Text0.LeftPadding + Text0.RightPadding + Text0.BorderWidth * 2 * 15

sums to 120, so

Text0.Width = Text0.Width + Text0.LeftPadding + Text0.RightPadding + Text0.BorderWidth * 2 * 15

would be be equivalent to

Text0.Width = Text0.Width + 120

The fact that you got it working with a magic number rather than my code (which should provide a pretty much the same result) suggests that you probably modified my code in such a way that

Text0.Width = Text0.Width + Text0.LeftPadding + Text0.RightPadding + Text0.BorderWidth * 2 * 15

didn't actually get run ...

RE: Expand text box width to fit text

(OP)
Actually I had no idea what WizHook was and still don't, that's how I found the code. I also had no idea what the 2 * 15 was for now I do and can modify the numbers a little to get it working. I entered exactly what you had given me as the example with the + Text0.BorderWidth * 2 * 15 modification and still received the same result. I'm going to go back and try adjusting the numbers a little and see what it does...

I have a second question and have no idea going about it. I am trying to create a switchboard menu that I can use for future databases and am having trouble with toggling the footer so that it shows only when certain selections are made and hides if they don't using a table with a Yes/No for each selection that I want to Collapse/Expand it. Should I start a new thread or pose the copy here and would you be able to help?

Thanks,

RE: Expand text box width to fit text

New thread for a new question

RE: Expand text box width to fit text

Couple of other ideas, instead of changing the size of the textbox, could you change the point size of the font, something along the lines of:
If number of characters > 30 then point size = 6 else point size = 8

strongm's code is more flexible, but if you don't mind hard coding some values, this is from an application where I had a data sheet form and used a formula to adjust the size of the col widths based on character width, of course it was predefined, so may not be of much use, but perhaps could be adapted? Below was based on 8 point Calibri

CODE -->

Const TWIPSTOINCHES = 1440
'TWIPS times (number of inches for 1 character at 8 point Calibri)
Const TWIPSTOCHARWIDTH = TWIPSTOINCHES * 0.06    
Me.txtFamilyTree.ColumnWidth = TWIPSTOCHARWIDTH * 5 'desired character width of 5 characters
Me.txtSearchTerm.ColumnWidth = TWIPSTOCHARWIDTH * 25 


RE: Expand text box width to fit text

(OP)
Strongm, I tried yours again and got it working by using the below:

' We need to add the padding
txtItemText.Width = txtItemText.Width + txtItemText.LeftPadding + txtItemText.RightPadding + txtItemText.BorderWidth * 2 * 15 + 50

Can you explain why? Thanks!!

RE: Expand text box width to fit text

There can be a number of reasons - for example, are you using a bold font, which would have wider characters? If so, you need to tell the function. My example was exactly that - an example of what you can do, not a complete solution, and thus did the bare minimum. But is set up to do more.

So in my code, we'd deal with this by changing

Text0.Width = WidthFromFontWiz(Text0.Text, Text0.FontName, Text0.FontSize)

to

Text0.Width = WidthFromFontWiz(Text0.Text, Text0.FontName, Text0.FontSize, Text0.FontWeight, Text0.FontItalic, Text0.FontUnderline)

RE: Expand text box width to fit text

>based on character width

based on average character width ... In these days of proportional fonts this is less useful than it might be

RE: Expand text box width to fit text

>Can you explain why? Thanks!!

And now that I have seen your project - yes, I can

You have a right margin set on the textbox. So the resizing needs to allow for that as well... your code would become

txtItemText.Width = txtItemText.Width + txtItemText.LeftPadding + txtItemText.RightPadding + txtItemText.BorderWidth * 2 * 15 + txtItemText.RightMargin + txtItemText.LeftMargin

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close