Text Width in VB 6

soclose picture soclose · Feb 21, 2012 · Viewed 7.5k times · Source

To check the Text Width, how to set the scale mode?

Is it -

    Debug.Print .ScaleMode = 1

Or

    Me.ScaleMode = 1

Which one does it work?

I test it with below code

Private Sub Command1_Click()
Dim xStr As String
    xStr = "W"
    With frmLabel
        .Font.Name = "Arial"
        .Font.Size = 10
        'Debug.Print .ScaleMode = 1

        '0 to 7
        Me.ScaleMode = 7
        Debug.Print .TextWidth(xStr) ' TextWidth = 435
        .Font.Size = 14
        Debug.Print .TextWidth(xStr) ' TextWidth = 645
    End With
End Sub

How could I define the kind of measure or unit? I'm looking for inch.

Thank you.

Answer

Bob77 picture Bob77 · Feb 21, 2012

In the context of a Form, UserControl, or UserDocument "Me" is already part of the namespace, so just use something like:

ScaleMode = vbInches

You can use redundant overqualification if you wish, as in:

Me.ScaleMode = vbInches

Since you can't write code inside of a PictureBox (and thus there is no local "Me" anyway) you are writing in the context of its container, so to set the property of a picScrollbox you'd write:

picScrollbox.ScaleMode = vbInches

The same is true for a Printer object.

But please avoid magic numbers and make use the intrinsic Enum ScaleModeConstants that provides symbolic names easier for the next guy to read.