soclose
soclose

Reputation: 2783

Text Width in VB 6

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.

Upvotes: 0

Views: 8679

Answers (2)

MarkJ
MarkJ

Reputation: 30408

Me.Font.Name = "Arial"
Me.Font.Size = 10
Me.ScaleMode = vbInch ' 5

TextWidth returns the width if the text were output using Print with the current Font of the Form. You aren't setting the Font of the Form in the code in your question. Set Me.Font. http://msdn.microsoft.com/en-us/library/aa267168(v=vs.60).aspx

The value is returned in units as determined by ScaleMode property of the form. The possible values of ScaleMode are in the docs http://msdn.microsoft.com/en-us/library/aa445668(v=vs.60).aspx

Another time why not try context-sensitive help? In the VB6 IDE code view put the cursor in ScaleMode and press F1 to go straight to the ScaleMode topic in the docs.

Upvotes: 0

Bob77
Bob77

Reputation: 13267

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.

Upvotes: 3

Related Questions