• We just launched and are currently in beta. Join us as we build and grow the community.

Draw a Text in a Windows Form in C#

Zionyx

Airdrop Hunter
Z Rep
0
0
0
Rep
0
Z Vouches
0
0
0
Vouches
0
Posts
168
Likes
154
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, I will teach you how to create a program that will draw a text in a windows form using C#.

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 and name your project as Draw a Text in Windows Form.

2. Next, add only one Button named Button1 and labeled it as "Draw a Text". You must design your interface like this:

drawdesign.png


3. In your button1_click as Draw a Text button, put this code below.

  1. public

    void

    Button1_Click(

    System

    .

    Object

    sender, System

    .

    EventArgs

    e)
  2. {

  3. StringFormat formatForDrawing =

    new

    StringFormat(

    )

    ;

  4. using

    (

    Graphics useGraphics =

    this

    .

    CreateGraphics

    (

    )

    )
  5. {
  6. using

    (

    System.Drawing

    .

    Font

    useFont =

    new

    System.Drawing

    .

    Font

    (

    "Garamond"

    , 35

    )

    )
  7. {
  8. using

    (

    SolidBrush useBrush =

    new

    SolidBrush(

    Color.

    DarkRed

    )

    )
  9. {

  10. useGraphics.

    DrawString

    (

    "Lyndon Bermoy"

    , useFont, useBrush, 50

    , 50

    , formatForDrawing)

    ;
  11. }
  12. }
  13. }
  14. }

We initialized 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.

We used also the Using statement to acquire the system resources that the block controls and initialized a useGraphics variable as Graphics out of it.Apart from it, we initialized a variable usefont as New System.Drawing.Font that has Garamond font and fonts size of 35. You can change this setting as you want it. We initialized also 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.

Then in the useGraphics variable use the DrawString property that has the text of "Lyndon Bermoy", the useFont variable that has Garamond font and 25 font size, the useBrush variable that has the dark red color of the text, x-coordinate and y-coordinate will be 50, and the variable formatForDrawing to draw this format of string.

Output:
drawoutput.png


For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below.

Best Regards,

Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer

If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]

Add and Follow me on Facebook: https://www.facebook.com/donzzsky

Visit and like my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

442,401

317,942

317,951

Top