We have learned how to draw rectangle, ellipse, and circle in visual basic 2019 in the preceding lessons, now we shall learn how to draw text on the screen. In order to draw text on the screen, we can use the DrawString method. The syntax is as follows:
myGraphics.DrawString(myText, myFont, mybrush, X , Y)
*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 the upper left corner of the Text.
You can create the Font object in visual basic 2019 using the following statement:
myFont = New System.Drawing.Font(“Verdana”, 20)
*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:
In this example, the program allows the user to enter the text he or she wishes to draw on the form in an InputBox. The text printed on the Form will be dark orchid in color, with typeface Arial, font size 18 and font style underline.
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 Dim myText As String myText = InputBox("Enter the text you wish to draw") myBrush = New Drawing.SolidBrush(Color.DarkOrchid) myFont = New System.Drawing.Font("Arial", 18, FontStyle.Underline) myGraphics.DrawString(myText, myFont, myBrush, 10, 10) End Sub
The InputBox appears after clicking the Draw Text button, as shown in Figure 29.1
After the user entered the text and click OK, the text is printed on the Form, as shown in Figure 29.2
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(myText, me.Font, System.Drawing.Brushes.DarkOrchid, 10, 10)
You can customize the InputBox by adding additional attributes such as the title, the default text and instruction for the user that prompt the user's action.Example 29.2
In this example, we change the Form font typeface to Algerian and font size to 14. The phrase printed on the Form will follow the font properties of the Form.
Private Sub BtnPrint_Click(sender As Object, e As EventArgs) Handles BtnPrint.Click Dim myGraphics As Graphics = Me.CreateGraphics Dim myPhrase As String myPhrase = InputBox("What is your message?", "Message Entry Form", "Enter your message here", 100, 200) myGraphics.DrawString(myPhrase, Me.Font, System.Drawing.Brushes.DarkOrchid, 10, 10) End Sub
The InputBox appears after clicking the Print Text button, as shown in Figure 29.3
After entering the phrase 'I LIKE VISUAL BASIC 2019' and click OK on the InputBox, the phrase will be printed on the Form,a shown in Figure 29.4.