Draw a Text in a Windows Form in VB.NET
Submitted by donbermoy on Friday, April 11, 2014 - 21:22.
In this article, we will create a program that can draw a text in a windows form when we click a button. We will use the Graphics and StringFormat class for this.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.
2. Next, add only one Button named Button1 and labeled it as "Draw a Text". You must design your interface like this:
3. In your button1_click as Draw a Text button, put this code below.
Initialize a variable formatForDrawing as a new StringFormat. StringFormat Class captures text layout information such as text alignment, and other properties of a text and display its operations.
Dim formatForDrawing As New StringFormat()
Use the Using statement to acquire the system resources that the block controls and initialized a useGraphics variable as Graphics out of it.
Using useGraphics As Graphics = Me.CreateGraphics(), _
Apart from it, initialized a variable usefont as New System.Drawing.Font that has Verdana font and fonts size of 20. You can change this setting as you want it.
useFont As New System.Drawing.Font("Verdana", 20), _
Initialized a variable useBrush as a Solid Brush that has a Color.Maroon inside in it. We just used this class to have the color of our text.
useBrush As New SolidBrush(Color.Maroon)
Then in the useGraphics variable use the DrawString property that has the text of "Sourcecodester", the useFont variable that has Verdana font and 20 font size, the useBrush variable that has the maroon color of the text, x-coordinate and y-coordinate will be 50, and the variable formatForDrawing to draw this format of string.
- useGraphics.DrawString("Sourcecodester", useFont, useBrush, _
- 50, 50, formatForDrawing)
- End Using
Output:
Download the source code below and try it! :) For more inquiries just contact my number or e-mail below. Best Regards, Engr. Lyndon R. Bermoy IT Instructor/System Developer/Android Developer Mobile: 09488225971 Telephone: 826-9296 E-mail:[email protected] Follow and add me in my Facebook Account: https://www.facebook.com/donzzsky Visit my page on Facebook at: https://www.facebook.com/BermzISwareAdd new comment
- 189 views