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
 

Use Solid Brush and rectangle to draw text on form in VB - C #.net.

Author: Maliha Atteeq
Download Source Code : 1076_code.zip

In this simple article you will learn that how to draw text on form using Solid Brush and rectangle in vb and C #.net.

 Graphics:

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:

Protected Overrides Sub OnPaint(ByVal As PaintEventArgs)
        
    
End Sub

In this article I will tell you that how can we draw a string on form using solid brush and rectangle.

Solid Brush:

Like a pen, the primary characteristic of a brush is its color. To help you create a simple brush, the System::Drawing namespace provides the static sealed Brushes class. The only feature this class provides is the ability to specify a color to use on a brush. As a static class, you never have to instantiate it. To create a simple brush whose only information is provided by its color, call the Brushes class and access a color by qualifying it with the name of the class. Each color is provided by its name as a property.

C#:

Brush brush = new SolidBrush(Color.OliveDrab);

VB:

Dim brush As Brush = New SolidBrush(Color.OliveDrab)

 

In order to display the graphics you have to override the function onpaint(). Create a graphics and a rectangle object. Also create an object of Solidbrush class, this class can’t be inherited.

You can create the object of pen class by using this constructor:

C#:

Pen pen = new Pen(Color.PaleGoldenrod);

VB:

Dim pen As New Pen(Color.PaleGoldenrod)

the next function is the drawString(). This function has five overloaded methods,depending ob your requirements you can use any of them. The return type of this function is void.

The syntax of the Drawstring() function is:

C#:

g.DrawString("SolidBrush", f, hb, 0, 0);

VB:

g.DrawString("SolidBrush", f, hb, 0, 0)

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

C#:

protected override void  OnPaint(PaintEventArgs e)

        {

            Graphics g = e.Graphics;

            int cx = ClientSize.Width;

            int cy = ClientSize.Height;

            Pen pen = new Pen(Color.PaleGoldenrod);

            Rectangle rect = new Rectangle(0, 0, cx / 2, cy / 2);

            g.DrawString("Solid Brush application", Font, new SolidBrush(Color.PaleTurquoise), rect);

            g.DrawLine(pen, 0, cy / 2, cx / 2, cy / 2);

            g.DrawLine(pen, cx / 2, 0, cx / 2, cy / 2);

 

        }        

VB:

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

   Dim g As Graphics = e.Graphics

Dim cx As Integer = ClientSize.Width

Dim cy As Integer = ClientSize.Height

Dim pen As New Pen(Color.PaleGoldenrod)

Dim rect As New Rectangle(0, 0, cx \ 2, cy \ 2)

g.DrawString("Solid Brush application", Font, New SolidBrush(Color.PaleTurquoise), rect)

g.DrawLine(pen, 0, cy \ 2, cx \ 2, cy \ 2)

g.DrawLine(pen, cx \ 2, 0, cx \ 2, cy \ 2)

End Sub

Now write the following code on FORM LOAD event:

C#:

private void Form1_Load(object sender, EventArgs e)

        {

                       this.Text = "DEVASP  APPLICATION";      

        }

VB:

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

  Me.Text = "DEVASP  APPLICATION"

This simple article tells that how to draw text on form using Solid Brush and rectangle in vb and C #.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Using Hatch Brush in VB.net - C #.net.

Disclaimer - Privacy
© 2002-2017 DevASP.net