auto resize a text in a text box

A

Arlene

Hi, is there a way to auto resize a text to fill the text box. I want to use
this for a name tag to fill up the text box according to name's length. Thank
you in advance.
 
M

Marshall Barton

Arlene said:
Hi, is there a way to auto resize a text to fill the text box. I want to use
this for a name tag to fill up the text box according to name's length. Thank
you in advance.


What do you mean by "resize"?

If you mean change its FontSize and it fits on one line,
then try something like:

Dim fs As Integer
For fs = 14 To 5 Step -1
If TextWidth(Me.thetextbox) < Me.thetextbox.Width Then
Exit For
End If
Next fs
Me.thetextbox.FontSize = fs
 
N

Nick Thompson

Hi

Sorry for digging up an old thread but I have the same problem as Arlen. Trying to use the code you've provided, Marsh, but it's not quite working as expected. Can you help?

This is my full code, inc debugging info

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)

Dim fs As Integer

Debug.Print Text0 & vbCrLf & "Height: " & Text0.Height & " Width: " & Text0.Width & " TextWidth/Height:" & TextWidth(Me.Text0) & "/" & TextHeight(Me.Text0) & " FONT SIZE: " & Me.Text0.FontSize

For fs = 25 To 5 Step -1
Me.Text0.FontSize = fs
If TextWidth(Me.Text0) < Me.Text0.Width Then
Exit For
End If
Next fs

Debug.Print "Height: " & Text0.Height & " Width: " & Text0.Width & " TextWidth/Height:" & TextWidth(Me.Text0) & "/" & TextHeight(Me.Text0) & " FONT SIZE: " & Me.Text0.FontSize & vbCrLf

End Sub


The debugging info this gives me is:

Short
Height: 559 Width: 5265 TextWidth/Height:480/269 FONT SIZE: 4
Height: 559 Width: 5265 TextWidth/Height:480/269 FONT SIZE: 25

Very very very very long indeed
Height: 559 Width: 5265 TextWidth/Height:2818/269 FONT SIZE: 25
Height: 559 Width: 5265 TextWidth/Height:2818/269 FONT SIZE: 25

Quite long text
Height: 559 Width: 5265 TextWidth/Height:1330/269 FONT SIZE: 25
Height: 559 Width: 5265 TextWidth/Height:1330/269 FONT SIZE: 25

(the first line in each section is the value in the text box).

All of the text comes out the same size. I would expect the second value to have resulted in small text, but it gets cropped instead. As you can see, the TextWidth of that text is 2818, which is less than the width of the textbox (5265). But as the text is longer than the text box I'd have expected it to have been higher than 5265.

I've also tried the same code in Report_Page event but I either get text at 25 points or 5 points - nowhere in between.

Thanks in advance if you can help!

Nick



Marshall Barton wrote:

Re: auto resize a text in a text box
10-Oct-08

Arlene wrote:



What do you mean by "resize"?

If you mean change its FontSize and it fits on one line,
then try something like:

Dim fs As Integer
For fs = 14 To 5 Step -1
If TextWidth(Me.thetextbox) < Me.thetextbox.Width Then
Exit For
End If
Next fs
Me.thetextbox.FontSize = fs

--
Marsh
MVP [MS Access]

EggHeadCafe - Software Developer Portal of Choice
Silverlight 2 Beta 2 - Doing Data Part I
http://www.eggheadcafe.com/tutorial...cc-58558a7e83f8/silverlight-2-beta-2--do.aspx
 
M

Marshall Barton

Nick Thompson wrote: [snipped to essentials]
Trying to use the code you've provided, Marsh, but it's not
quite working as expected.

Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
Dim fs As Integer

For fs = 25 To 5 Step -1
Me.Text0.FontSize = fs
If TextWidth(Me.Text0) < Me.Text0.Width Then
Exit For
End If
Next fs
End Sub

All of the text comes out the same size. I would expect the second value to have resulted in small text, but it gets cropped instead. As you can see, the TextWidth of that text is 2818, which is less than the width of the textbox (5265). But as the text is longer than the text box I'd have expected it to have been higher than 5265.


Nothing in that code tells TextWidth what font size to use
so it's always using whatever the report's default font
size. I should be more like:

Dim fs As Integer
For fs = 14 To 5 Step -1
Me.FontSize = fs
If TextWidth(Me.thetextbox) < Me.thetextbox.Width Then
Exit For
End If
Next fs
Me.thetextbox.FontSize = fs
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top