Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Draw a string on form using Graphics class in vb.net - C #.net.

Author: Maliha Atteeq
Download Source Code : 1031_grapsApps.zip

In this simple article you will learn that how to draw a string on the form using Graphics class in vb and C #.net.

 Graphics:

This is the very important class for making graphics of your own using c#. This class cannot be inherited. This class provides us a number of different methods for graphics creation. By using these methods you can create graphics of your own choice.

In this article I will tell you that how can we draw a String using system.drawing.graphics.

String draw:

In order to draw a string on the form, firstly you should know the behavior of the form. While playing with the graphics you must keep the following point in your mind:

You can’t draw graphics on simple load event of the form. To create graphics on the form, you must override the onpaint() function of the form.

The syntax of the onpaint() function is:

C#:

protected override void OnPaint (PaintEventArgs e)

{

}

VB:

Dim e As PaintEventArgs

 

Me.OnPaint(e)

String’s draw function:

In order to draw a string, graphics class provides us a function named as drawString(). It is an overloaded function. By using this function you can draw a string on the form. Depending on your requirement you can use any of the overloaded function. I use the following:

This function takes four arguments. The first argument is the object of Brush class. Brush class is an abstract class; you can use any concrete class of it. I use the solid brush class which is the concrete class of the brush.

C#:

System.Drawing.SolidBrush brush = new SolidBrush(Color.DarkGoldenrod);

VB:

Dim brush As System.Drawing.SolidBrush = New SolidBrush(Color.DarkGoldenrod)

The second argument is the object of string class. You can declare the string in the constructor of this class which you want to draw on the form. The third argument is font of class type font. This class has also overloaded constructor. Depending on your requirements you can use any of them. I use the following.

 C#:

System.Drawing.Font font = new Font(family, emsize);

VB:

Dim font As System.Drawing.Font = New Font(family, emsize)

 

The third argument is of type pointf of class pointF. It takes two floating point argument as a parameter.

 

C#:

System.Drawing.PointF point = new PointF(x, y);

 

VB:

Dim point As System.Drawing.PointF = New PointF(x, y)

 

Pass all these arguments in the DrawString() function provided by the graphics class. Once you do this then you have to call the Dispose() method to epose your created string. The dispose() function takes no argument as a paameter. This function simply expose your graphics on the form. You have to call this function with the object of graphics and also with the object of pen class.

 

C#:

       obj.Dispose();

 

VB:

               obj.Dispose();

 

To demonstrate make a new window application and write the following code on form’s onpaint event:

C#:

protected override void OnPaint(PaintEventArgs e)

        {

            System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.Black);

            System.Drawing.Graphics obj;

            obj = this.CreateGraphics();

            float x = 25f;

            float y = 35f;

            String str = "String Draw Function";

            System.Drawing.SolidBrush brush = new SolidBrush(Color.DeepPink);

            System.Drawing.PointF point = new PointF(x, y);

            String family = "My Font";

            float emsize = 20f;

            System.Drawing.Font font = new Font(family, emsize);

            obj.DrawString(str,font,brush,point);

            obj.Dispose();

        }

VB:   

Protected Overloads Overrides Sub OnPaint(ByVal e As PaintEventArgs)

        Dim pen As New System.Drawing.Pen(System.Drawing.Color.Black)

        Dim obj As System.Drawing.Graphics

        obj = Me.CreateGraphics()

        Dim x As Single = 25.0F

        Dim y As Single = 35.0F

        Dim str As [String] = "String Draw Function"

        Dim brush As System.Drawing.SolidBrush = New SolidBrush(Color.DeepPink)

        Dim point As System.Drawing.PointF = New PointF(x, y)

        Dim family As [String] = "My Font"

        Dim emsize As Single = 20.0F

        Dim font As System.Drawing.Font = New Font(family, emsize)

        obj.DrawString(str, font, brush, point)

        obj.Dispose()

    End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP STRING DRAW APPLICATION";

           

        }

Vb:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

 

        Me.Text = "DEVASP STRING DRAW APPLICATION"

    End Sub

This simple article tells that how to draw a filled rectangle using Graphics class in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Draw a filled rectangle on the form using Graphics class in vb.net - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net