Export (0) Print
Expand All

How to: Display a Box Around Text

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The following example program draws a rectangle around a string of text.


Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
    Dim s As String = ".NET Compact Framework"
    Dim pen As New Pen(Color.Red, 5)
    Dim font As New Font("Arial", 10, FontStyle.Regular)
    Dim brush As New SolidBrush(Color.Black)
    Dim textSize As SizeF = e.Graphics.MeasureString(s, font)

    ' Create a rectangle with padding space between string and box.
    Dim r As New Rectangle(45, 70, CInt(Fix(textSize.Width) + 10), _
        CInt(Fix(textSize.Height) + 10))
    e.Graphics.DrawRectangle(pen, r)
    e.Graphics.DrawString(s, font, brush, 50F, 75F)
    MyBase.OnPaint(e)
End Sub


This example requires references to the following namespaces:

Show:
© 2014 Microsoft