Click here to Skip to main content
15,891,375 members
Articles / Multimedia / GDI+

Measuring strings

Rate me:
Please Sign up or sign in to vote.
3.55/5 (20 votes)
29 Sep 20042 min read 169.2K   651   21  
An example of using the Graphics.MeasureString() method.
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Imports System.Drawing.Text
Imports System.Data.SqlClient

Public Class frmMain
  Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.IContainer

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  Friend WithEvents txtText As System.Windows.Forms.TextBox
  Friend WithEvents cmdSetWL As System.Windows.Forms.Button
  Friend WithEvents Label1 As System.Windows.Forms.Label
  Friend WithEvents Label2 As System.Windows.Forms.Label
  Friend WithEvents txtCW As System.Windows.Forms.TextBox
  Friend WithEvents txtWL As System.Windows.Forms.TextBox
  Friend WithEvents txtFS As System.Windows.Forms.TextBox
  Friend WithEvents Label3 As System.Windows.Forms.Label
  Friend WithEvents Label4 As System.Windows.Forms.Label
  Friend WithEvents FontDialog1 As System.Windows.Forms.FontDialog
  Friend WithEvents txtFN As System.Windows.Forms.TextBox
  Friend WithEvents cmdFont As System.Windows.Forms.Button
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.cmdSetWL = New System.Windows.Forms.Button()
    Me.txtText = New System.Windows.Forms.TextBox()
    Me.Label1 = New System.Windows.Forms.Label()
    Me.Label2 = New System.Windows.Forms.Label()
    Me.txtCW = New System.Windows.Forms.TextBox()
    Me.txtWL = New System.Windows.Forms.TextBox()
    Me.txtFS = New System.Windows.Forms.TextBox()
    Me.Label3 = New System.Windows.Forms.Label()
    Me.Label4 = New System.Windows.Forms.Label()
    Me.FontDialog1 = New System.Windows.Forms.FontDialog()
    Me.txtFN = New System.Windows.Forms.TextBox()
    Me.cmdFont = New System.Windows.Forms.Button()
    Me.SuspendLayout()
    '
    'cmdSetWL
    '
    Me.cmdSetWL.Location = New System.Drawing.Point(140, 116)
    Me.cmdSetWL.Name = "cmdSetWL"
    Me.cmdSetWL.Size = New System.Drawing.Size(76, 20)
    Me.cmdSetWL.TabIndex = 10
    Me.cmdSetWL.Text = "Set Limit"
    '
    'txtText
    '
    Me.txtText.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right)
    Me.txtText.Location = New System.Drawing.Point(8, 8)
    Me.txtText.Name = "txtText"
    Me.txtText.Size = New System.Drawing.Size(208, 20)
    Me.txtText.TabIndex = 0
    Me.txtText.Text = "MyText"
    '
    'Label1
    '
    Me.Label1.Location = New System.Drawing.Point(8, 96)
    Me.Label1.Name = "Label1"
    Me.Label1.Size = New System.Drawing.Size(76, 20)
    Me.Label1.TabIndex = 6
    Me.Label1.Text = "Current width:"
    '
    'Label2
    '
    Me.Label2.Location = New System.Drawing.Point(8, 120)
    Me.Label2.Name = "Label2"
    Me.Label2.Size = New System.Drawing.Size(76, 20)
    Me.Label2.TabIndex = 8
    Me.Label2.Text = "Width limit:"
    '
    'txtCW
    '
    Me.txtCW.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.txtCW.Location = New System.Drawing.Point(84, 92)
    Me.txtCW.Name = "txtCW"
    Me.txtCW.ReadOnly = True
    Me.txtCW.Size = New System.Drawing.Size(46, 20)
    Me.txtCW.TabIndex = 7
    Me.txtCW.Text = "0"
    Me.txtCW.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
    '
    'txtWL
    '
    Me.txtWL.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.txtWL.Location = New System.Drawing.Point(84, 116)
    Me.txtWL.Name = "txtWL"
    Me.txtWL.ReadOnly = True
    Me.txtWL.Size = New System.Drawing.Size(46, 20)
    Me.txtWL.TabIndex = 9
    Me.txtWL.Text = "0"
    Me.txtWL.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
    '
    'txtFS
    '
    Me.txtFS.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.txtFS.Location = New System.Drawing.Point(84, 60)
    Me.txtFS.Name = "txtFS"
    Me.txtFS.ReadOnly = True
    Me.txtFS.Size = New System.Drawing.Size(46, 20)
    Me.txtFS.TabIndex = 4
    Me.txtFS.Text = "18"
    Me.txtFS.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
    '
    'Label3
    '
    Me.Label3.Location = New System.Drawing.Point(8, 64)
    Me.Label3.Name = "Label3"
    Me.Label3.Size = New System.Drawing.Size(76, 20)
    Me.Label3.TabIndex = 3
    Me.Label3.Text = "Font size:"
    '
    'Label4
    '
    Me.Label4.Location = New System.Drawing.Point(8, 40)
    Me.Label4.Name = "Label4"
    Me.Label4.Size = New System.Drawing.Size(76, 20)
    Me.Label4.TabIndex = 1
    Me.Label4.Text = "Font name:"
    '
    'txtFN
    '
    Me.txtFN.Anchor = ((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                Or System.Windows.Forms.AnchorStyles.Right)
    Me.txtFN.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    Me.txtFN.Location = New System.Drawing.Point(84, 36)
    Me.txtFN.Name = "txtFN"
    Me.txtFN.ReadOnly = True
    Me.txtFN.Size = New System.Drawing.Size(132, 20)
    Me.txtFN.TabIndex = 2
    Me.txtFN.Text = "Verdana"
    '
    'cmdFont
    '
    Me.cmdFont.Location = New System.Drawing.Point(140, 60)
    Me.cmdFont.Name = "cmdFont"
    Me.cmdFont.Size = New System.Drawing.Size(76, 20)
    Me.cmdFont.TabIndex = 5
    Me.cmdFont.Text = "Set Font"
    '
    'frmMain
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(224, 153)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdFont, Me.txtFN, Me.Label4, Me.txtFS, Me.Label3, Me.txtWL, Me.txtCW, Me.Label2, Me.Label1, Me.txtText, Me.cmdSetWL})
    Me.MaximizeBox = False
    Me.MinimizeBox = False
    Me.Name = "frmMain"
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
    Me.Text = "Strings Meter"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler txtText.TextChanged, AddressOf txtText_TextChanged
    txtText_TextChanged(Nothing, Nothing)
    cmdSetWL_Click(Nothing, Nothing)
  End Sub

  Private Sub cmdSetWL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetWL.Click
    txtWL.Text = txtCW.Text
    txtCW.BackColor = Color.Gray
  End Sub

  Private Sub cmdFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFont.Click
    If FontDialog1.ShowDialog() = DialogResult.OK Then
      txtFN.Text = FontDialog1.Font.Name
      txtFS.Text = FontDialog1.Font.Size.ToString()
      txtText_TextChanged(Nothing, Nothing)
    End If
  End Sub

  Private Sub txtText_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim w, h As Integer
    Measure(txtText.Text, txtFN.Text, Single.Parse(txtFS.Text), w, h)
    txtCW.Text = w.ToString()
    If w > Single.Parse(txtWL.Text) Then
      txtCW.BackColor = Color.Red
    Else
      txtCW.BackColor = Color.Gray
    End If
  End Sub

  Private Sub Measure(ByVal BannerText As String, ByVal FontName As String, ByVal FontSize As Single, ByRef Width As Single, ByRef Height As Single)

    Dim b As Bitmap
    Dim g As Graphics
    Dim f As New Font(FontName, FontSize)

    ' Compute the string dimensions in the given font
    b = New Bitmap(1, 1, PixelFormat.Format32bppArgb)
    g = Graphics.FromImage(b)
    Dim stringSize As SizeF = g.MeasureString(BannerText, f)
    Width = stringSize.Width
    Height = stringSize.Height
    g.Dispose()
    b.Dispose()

  End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Technical Lead
Italy Italy
I was born in 1970.

My first computer experience dates back to early 80s, with a Sinclair ZX81.
From that time on, as many "friends" say, my IT-illness has increased year by year.

I graduated in Electronic Engineering and earned the following Microsoft certifications:
MCP, MCT, MCDBA, MCSD, MCAD, MCSD for .NET (early achiever).

I worked in IT as a developer, a teacher, a consultant, a technical writer, a technical leader.
IT knowledge applied to real life is my primary interest and focus.

Comments and Discussions