Hi, y'all,
I am trying to determine the length of a string in pixels.
I created a little test program for myself and placed three labels on it, each with the same font in a different size. I set the width of the label to 1024, the screenwidth. I used the same test text for each label, cutting it off at the last character that shows, so there is not hidden text running off the label to be included in the string width count.
What I have found is that the width of the string is being reported as wider than the label, even though, as I just mentioned, there are no characters running off the edge of the label. So, something's awry. Does anyone know how to get an accurate width?
Here follows the whole test code. You can drop three labels on a form and run it to see what I mean.
SQL
Option Explicit On
Option Strict On
Public Class frmMain
Dim StringSize AsNew SizeF
Dim StringSizeForLargerFont AsNew
SizeF
Dim StringSizeForLargestFont AsNew SizeF
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
lblMessage.Text = "1 2 3 4 5 6 7 8 9 0 A 1 2 3 4 5 6 7 8 9 0 B 1 2 3 4 5 6 7
8 9 0 C " & _
"1 2 3 4 5 6 7 8 9 0 D 1 2 3 4 5 6 7 8 9 0 E 1 2 3 4 5 6 7
8 9 0 F 1 2 3 4 5 6 7 8 9 0 G " & _
"1 2 3 4 5 6 7 8 9 0 H 1 2 3 4 5 6 7
8 9"
lblMessage.Width =1280
lblMessage.Font =New
System.Drawing.Font("Trebuchet MS", 12)
lblMessage.Font =New
System.Drawing.Font(lblMessage.Font, FontStyle.Regular)
lblMessageInLargerFont.Text = "1 2 3 4 5 6 7 8 9 0 A 1 2 3 4 5 6 7 8 9 0 B 1
2 3 4 5 6 7 8 9 0 C " & _
"1 2 3 4 5 6 7 8 9 0 D 1 2 3 4 5 6 7 8 9 0 E 1
2 3 4 5 6 7 8 9 0 F"
lblMessageInLargerFont.Width =1280
lblMessageInLargerFont.Font =New System.Drawing.Font("Trebuchet MS",
17.25)
lblMessageInLargerFont.Font =New
System.Drawing.Font(lblMessageInLargerFont.Font, FontStyle.Regular)
lblMessageInLargestFont.Text = "1 2 3 4 5 6 7 8 9 0 A 1 2 3 4 5 6 7 8 9 0 B 1
2 3 4 5 6 7 8 9 0 C " & _
"1 2 3 4 5 6 7 8 9 0 D 1 2 3 4 5 6 7 8 9 0 E 1
2"
lblMessageInLargestFont.Width =1280
lblMessageInLargestFont.Font =New
System.Drawing.Font("Trebuchet MS", 20.25)
lblMessageInLargestFont.Font =New
System.Drawing.Font(lblMessageInLargestFont.Font, FontStyle.Regular)
End Sub
Private Sub frmMain_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.PageUnit = GraphicsUnit.Pixel
StringSize = e.Graphics.MeasureString(lblMessage.Text,
lblMessage.Font)
StringSizeForLargerFont =
e.Graphics.MeasureString(lblMessageInLargerFont.Text,
lblMessageInLargerFont.Font)
StringSizeForLargestFont =
e.Graphics.MeasureString(lblMessageInLargestFont.Text,
lblMessageInLargestFont.Font)
lblInformation.Text = _
"Screen Dimensions: " & GetScreenWidth() &
" X " & GetScreenHeight() & vbCrLf & _
"lblMessage Width: " &
lblMessage.Width & vbCrLf & _
"Message String Width: " &
StringSize.ToString & vbCrLf & _
"Larger Message String Width: "
& StringSizeForLargerFont.ToString & vbCrLf & _
"Largest Message
String Width: " & StringSizeForLargestFont.ToString
End Sub
Public Function GetScreenHeight() AsInteger
Dim intY AsInteger= Screen.PrimaryScreen.Bounds.Height
Return intY
EndFunction
Public Function GetScreenWidth() AsInteger
Dim intX AsInteger= Screen.PrimaryScreen.Bounds.Width
Return intX
EndFunctionEnd Class
I appreciate input.
Answers
Know the answer? Post it. Somebody with the same question will find it here.
Answers
Know the answer? Post it. Somebody with the same question will find it here.