Lesson 29: Drawing Text in VB2019
Master text rendering with GDI+ using DrawString method
Key Takeaway
Visual Basic 2019 uses the DrawString method for text rendering, allowing you to customize fonts, colors, and positions for creating dynamic text elements.
After mastering shapes like rectangles and ellipses, we'll now explore how to draw text in VB2019. Text rendering is essential for creating user interfaces, annotations, and custom visual elements in your applications.
Font Fundamentals
Create custom font objects with specific typefaces and styles
Brush Options
Use various brush types to customize text color
Text Positioning
Place text at precise coordinates on your canvas
Advanced Styling
Apply bold, italic, underline and other text effects
29.1 Drawing Text in VB2019
In Visual Basic 2019, you can draw text using the DrawString method, which requires four essential components: the text content, a font object, a brush for coloring, and position coordinates.
Text Drawing Fundamentals
The DrawString method has the following syntax:
myGraphics.DrawString(myText, myFont, myBrush, X, Y)
| Parameter | Description | Required |
|---|---|---|
| myText | The text string to display | Yes |
| myFont | Font object defining typeface, size, and style | Yes |
| myBrush | Brush object defining text color | Yes |
| X, Y | Coordinates for the top-left corner of the text | Yes |
Creating Font Objects
Font objects define the appearance of your text. You can create them with various styles:
' Basic font Dim font1 As New Font("Arial", 12) ' Bold font Dim font2 As New Font("Verdana", 14, FontStyle.Bold) ' Italic font Dim font3 As New Font("Times New Roman", 16, FontStyle.Italic) ' Combined styles Dim font4 As New Font("Comic Sans", 18, FontStyle.Bold Or FontStyle.Underline)
Creating Brush Objects
Brush objects determine the color of your text. You can choose from hundreds of predefined colors:
' Create custom brush Dim myBrush As Brush = New Drawing.SolidBrush(Color.DarkOrchid) ' Use predefined brush Dim redBrush As Brush = System.Drawing.Brushes.Red ' Common color options: Color.AliceBlue, Color.AquaMarine, Color.Beige, Color.DarkMagenta, Color.DarkOliveGreen, Color.SkyBlue
Example 29.1: Basic Text Drawing
This example creates a dark orchid text with Arial font, size 18, and underline style:
Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click ' Create the Graphics object Dim myGraphics As Graphics = Me.CreateGraphics ' Get text from user Dim myText As String = InputBox("Enter the text you wish to draw") ' Create a dark orchid brush Dim myBrush As Brush = New Drawing.SolidBrush(Color.DarkOrchid) ' Create Arial font, size 18, with underline Dim myFont As New Font("Arial", 18, FontStyle.Underline) ' Draw the text at position (10, 10) myGraphics.DrawString(myText, myFont, myBrush, 10, 10) End Sub
Figure 29.1: InputBox for text entry
Figure 29.2: Text drawn on form with custom font and color
Example 29.2: Customized InputBox
This example uses a customized InputBox with a title and default text, and draws text using the form's font properties:
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click ' Create the Graphics object Dim myGraphics As Graphics = Me.CreateGraphics ' Get text from customized InputBox Dim myPhrase As String = InputBox( "What is your message?", ' Prompt "Message Entry Form", ' Title "Enter your message here", ' Default text 100, 200) ' X and Y position ' Draw using form's font and predefined brush myGraphics.DrawString(myPhrase, Me.Font, System.Drawing.Brushes.DarkOrchid, 10, 10) End Sub
Figure 29.3: Customized InputBox with title and default text
Figure 29.4: Text drawn using form's font properties
| Approach | When to Use | Advantages |
|---|---|---|
| Custom Font/Brush | When you need specific styling | Full control over appearance |
| Form Font/Predefined Brush | When you want consistency with UI | Simpler code, matches form style |
29.2 Advanced Text Options
Beyond basic text rendering, VB2019 offers advanced options for text formatting and alignment.
Text Formatting Options
You can control text alignment and formatting using the StringFormat class:
Private Sub BtnDrawFormatted_Click(sender As Object, e As EventArgs) Handles BtnDrawFormatted.Click Dim myGraphics As Graphics = Me.CreateGraphics Dim text As String = "Centered Text with Formatting" ' Create font and brush Dim myFont As New Font("Georgia", 20, FontStyle.Bold) Dim myBrush As Brush = Brushes.DarkBlue ' Create formatting options Dim format As New StringFormat() format.Alignment = StringAlignment.Center ' Horizontal center format.LineAlignment = StringAlignment.Center ' Vertical center ' Define drawing rectangle Dim rect As New Rectangle(10, 10, Me.Width - 20, Me.Height - 20) ' Draw centered text myGraphics.DrawString(text, myFont, myBrush, rect, format) End Sub
| StringFormat Property | Description | Options |
|---|---|---|
| Alignment | Horizontal text alignment | Near (left), Center, Far (right) |
| LineAlignment | Vertical text alignment | Near (top), Center, Far (bottom) |
| FormatFlags | Additional formatting options | DirectionVertical, NoWrap, etc. |
Lesson Summary
In this lesson, you've learned how to create and customize text in VB2019:
DrawString Method
Essential method for rendering text on graphics surfaces
Font Creation
Customize typeface, size, and style for your text
Brush Options
Choose from hundreds of colors for text rendering
Text Positioning
Place text at precise coordinates with advanced alignment
With text drawing mastered, you're ready to explore more complex shapes. In the next lesson, we'll learn to draw polygons and pie shapes.
Next Lesson
Learn to draw complex shapes in Lesson 30: Drawing Polygon & Pie.
Related Resources
Visual Basic 2019 Made Easy
Master Visual Basic 2019 with this comprehensive guide that includes detailed coverage of text rendering and graphics programming. Learn to create professional applications with custom text elements.
Key Graphics Topics:
- Text rendering with DrawString method
- Custom font creation and styling
- Color management with brushes
- Text alignment and formatting options
- Practical projects with full source code
Visual Basic Programming With Code Examples
This comprehensive guide includes numerous examples of text rendering in both VB6 and VB.NET, helping you understand the evolution of text capabilities in Visual Basic.
Text Rendering Coverage:
- 15+ text rendering examples
- Step-by-step text tutorials
- Advanced formatting techniques
- Dynamic text generation