PDA

View Full Version : autosize text in a label



Trevor
03-27-2008, 04:10 PM
Is there a way to autosize text so that it will fit inside a given lable
I would like to have a dlookup to lookup text for a lable and then on load of form the dlookup will then place the text in the label and autosize it for the best fit, this is so the user can change the text in a lable (just for user entertainment)
Thanks for helping

DarkSprout
03-28-2008, 02:55 AM
I just threw this together.
Seems to do the trick...
place the Sub in the form code.

Private Sub AutoSize(ctl As String)
'// Call using:
'// AutoSize "LabelName"
Me.Controls(ctl).Width = Len(Me.Controls(ctl).Caption) * (Me.Controls(ctl).FontSize * 13)
End Sub

ben.oates
03-28-2008, 07:36 AM
I think he wanted to change the size of the font, rather than the width of the label... so using DarkSprout's code it would be:


Private Sub AutoSize(ctl As String)
'// Call using:
'// AutoSize "LabelName"
Me.Controls(ctl).FontSize = Me.Controls(ctl).Width / (Len(Me.Controls(ctl).Caption) * 13 )
End Sub

Sorry if I got the wrong end of the stick. Also, this code isn't tested! I'm making assumptions from DarkSprout.

Trevor
03-28-2008, 01:50 PM
ok thanks you two ben.oats got what I wantd though, the label I want to auto change text size ie label151 so using your code , it should be



Private Sub AutoSize ()
Me.Label1w1.FontSize = Me.Label151.with / (Len(Me.Label151).Caption) *13)

but By looking at the code what does the *13 do
thanks

ben.oates
03-31-2008, 01:48 AM
The bracket in the middle here: Me.Label151).Caption

That needs to be removed.

I just transposed DarkSprout's original post. The * 13 will just make the text smaller here. Play with the number until it seems to work for you.




Private Sub AutoSize()
Me.label151.FontSize = Me.label151.Width / (Len(Me.label151.Caption) * 13 )
End Sub

Trevor
03-31-2008, 10:19 AM
Thanks, will try

Trevor
03-31-2008, 11:39 AM
That works thanks, I am thinking about doing the same for images in an image from , so the user(well admin) would select the largest image required then it would auto size to the size of the frame, I assume the same method as used for the text applies to Images but wouldn't I also have to controle hight as well as width?

ben.oates
04-01-2008, 02:23 AM
In VBA Access you can use the ImageHeight and ImageWidth property (http://msdn2.microsoft.com/en-us/library/aa196123(office.11).aspx).