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
 

Drawing a Rectangle on Windows Form Using System.Drawing.Graphics

Author: DevASP
Download Source Code : 489_Draw_Rectangle.zip

In this simple article you will learn that how you can draw a Rectangle on Windows Form Using Visual Basic.Net.

Simply just create New Project of windows application in VB.Net and drag two labels and a timer on form. Is create some global variables:

 

    Dim x1 As Integer = 10

    Dim x2 As Integer = 10

    Dim y1 As Integer = 50

    Dim y2 As Integer = 50

Now write code in following events:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

        Me.BackColor = Color.LightGoldenrodYellow

        Me.WindowState = FormWindowState.Maximized

 

        Me.Text = "DevAsp"

        Label1.Location = New System.Drawing.Point(Me.Width / 2 - Label1.Width / 2, 50)

        Label2.Location = New System.Drawing.Point(Me.Width / 2 - Label1.Width / 2, 150)

 

End Sub

 

 

Private Sub Form2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint

 

        Dim myPen As Pen

        myPen = New Pen(Color.Blue, 2)

        Dim gr As System.Drawing.Graphics = e.Graphics

        gr.DrawRectangle(myPen, x1, y1, Me.MousePosition.X - 200, Me.MousePosition.Y - 300)

        Timer1.Start()

 

End Sub

 

 

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

 

        Me.BackColor = Color.LightGray

        Me.BackColor = Color.LightGreen

        Me.BackColor = Color.LightGray

 

End Sub

 

 

Private Sub Form2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

 

        If e.Button = MouseButtons.Left Then

            Timer1.Stop()

        End If

        If e.Button = MouseButtons.Right Then

            Timer1.Start()

        End If

 

End Sub

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to draw a line on Form which follows your mouse in VB.Net using System.Drawing.Graphics

Disclaimer - Privacy
© 2002-2017 DevASP.net