Actual size of a displayed string

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 16:59
Joined
Feb 28, 2001
Messages
30,566
Problem: In order to make certain things look a little bit neater and more professional, I'm trying to dynamically resize text boxes on my forms. But the trick isn't doing the resizing, it is know when it is necessary to do so.

Is there some reasonable way to know that a given text string in a given font in a given font-size will take up 1.5 inches (therefore only needing one line in the displayed 2-inch text box) or that it will take up 2.3 inches (therefore needing two lines in the 2-inch text box)?

I am currently using a variable-width format so I can't just compute this width based on the font size times the number of characters. I.e. for Courier New or Lucida Console, which are non-proportional fonts, I could compute text size as font width times string character count. But with proportional fonts, that math doesn't quite work. Lower-case I and L in proportional fonts can get pretty skinny.

Any suggestions?
 
I don't know of any way to do what you're doing unless you take the time to build a library of some kind that knows the size of each character in each font. You would then have to iterate through all of the text and look up it's size in your CharacterSize table. I don't think this is worth it. Just use Courier New or Lucida Console.
 
I agree - when using a proportional font, you have quite a challenge there. If you use a fixed font, then it isn't so much a problem.
 
Try something like this:
Code:
Option Compare Database
Option Explicit

Private prevWidth As String

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
    If PrintCount = 1 Then
        Dim currWidth As Long
        
        With Me
            .ScaleMode = 1
            .FontName = .[COLOR=Red]txtBoxName[/COLOR].FontName
            .FontSize = .[COLOR=Red]txtBoxName[/COLOR].FontSize
        End With

        currWidth = Me.TextWidth(Me.[COLOR=Red]txtBoxName[/COLOR]) + 105

        If prevWidth < currWidth Then
            prevWidth = currWidth
            Me.[COLOR=Red]txtBoxName[/COLOR].Width = currWidth
        End If
    End If
End Sub

Private Sub Report_Load()
    prevWidth = Me.[COLOR=Red]txtBoxName[/COLOR].Width
End Sub
105 just adds that level of uncertainty. I've used 105 for Calibri so it might be an idea to limit your users to a certain number of font types and use a lookup table that will return the level of uncertainty based on the font type used.
 
Thanks, gang. I'll report back when I give this a try. I would have gotten back to this sooner but suddenly got avalanched by arrival of a new person who needed some fast training - and of course I'm the only one who can provide that training, per Murphy's Laws on availability of support personnel being inversely proportional to the current level of manager's perceived crisis. (Note I said "perceived" not "actual.")
 

Users who are viewing this thread

Back
Top Bottom