Visual Basic 2013 Lesson 28: Creating Graphics-Drawing Text

WhatsAppTwitterGoogle+Google GmailShare

[Lesson 27] << [Contents] >> [Lesson 29]

We have learned how to draw rectangle, ellipse and circle in visual basic 2013 in the preceding lessons, now we shall learn how to draw text on the screen. Yes, instead of using the Print command, you can also draw text on the screen.

28.1 Drawing Text

In order to draw text on the screen, we can use the DrawString method. The format is as follows:

myGraphics.DrawString(myText, myFont, mybrush, X , Y)

Where myGraphics is the Graphics object, myText is the text you wish to display on the screen, myFont is the font object created by you, myBrush is the brush style created by you and X, Y are the coordinates of upper left corner of the Text.

You can create the Font object in visual basic 2013 using the following statement:

myFont = New System.Drawing.Font(“Verdana”, 20)

Where the first argument of the font is the font typeface, and the second argument is the font size. You can add a third argument as font style, either bold, italic, underline. Here are the examples:

myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Bold)myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Underline)

myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Italic)

myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Regular)

To create your Brush object, you can use the following statement:

Dim myBrush As Brush

myBrush = New Drawing.SolidBrush(Color.BrushColor)


Besides the seven colors, some of the common Brush Colors are AliceBlue, AquaMarine Beige, DarkMagenta, DrarkOliveGreen, SkyBlue and more. You don’t have to remember the names of all the colors, the intelliSense will let you browse through the colors in a drop-down menu once you type the dot after the word Color.

Now we shall proceed to draw the font using the sample code below:

Example 28.1

Private Sub BtnDraw_Click(sender As Object, e As EventArgs) Handles BtnDraw.Click

Dim myGraphics As Graphics = Me.CreateGraphics
Dim myFont As Font
Dim myBrush As Brush

myBrush = New Drawing.SolidBrush(Color.DarkOrchid)
myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Underline)
myGraphics.DrawString(“Visual Basic 2013″, myFont, myBrush, 10, 10)

End Sub

The runtime interface is as shown in Figure 28.1

vb2013_figure28.1

 Figure 28.1


The preceding example can be modified if you don’t want to create the Font and the Brush objects. You can use the font of an existing object such as the Form and the System Colors. Replace the last line in the preceding example with this line(you need to delete the lines that create the Brush and the Font objects as well)

myGraphics.DrawString(“Visual Basic 2013″, me.Font, System.Drawing.Brushes.DarkOrchid, 10, 10)

You can also add a InputBox which let the user enter his or her message then display the message on the screen.

This is shown in Example 28.2

Example 28.2

Dim myGraphics As Graphics = Me.CreateGraphics

Dim myFont As Font

Dim myBrush As Brush

Dim userMsg As String

userMsg = InputBox(“What is your message?”, “Message Entry Form”, “Enter your message here”, 100, 200)

myBrush = New Drawing.SolidBrush(Color.DarkOrchid)

myFont = New System.Drawing.Font(“Verdana”, 20, FontStyle.Underline)

myGraphics.DrawString(userMsg, myFont, myBrush, 10, 10)

 

[Lesson 27] << [Contents] >> [Lesson 29]

WhatsAppTwitterGoogle+Google GmailShare

Please write your comment and share your ideas here :