The following example will draw a red arrow on windows form.
Private Sub Form1_Paint(ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim grp As Graphics = e.Graphics Dim TempPen As New Pen(Color.Red, 3) TempPen.StartCap = Drawing2D.LineCap.ArrowAnchor grp.DrawLine(TempPen, 100, 50, 20, 50)y End Sub
The above example is using Paint event of the Window form and it occurs when a form is drawn. The Paint event has ‘PaintEventArgs’ argument which gives the Graphics object. The Graphics object provides methods for drawing objects, In the above we are using the ‘DrawLine()’ method to draw the arrow.
Let’s take an another example which will demonstrate handling the Paint event and using the PaintEventArgs class to draw rectangles on the window form.
Private Sub Form1_Paint(ByVal sender As Object, _ ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint Dim RectangleObject As Rectangle 'specify the X coordinate RectangleObject.X = 100 'specify the Y coordinate RectangleObject.Y = 100 'specify the height of rectangle RectangleObject.Height = 200 'specify the width of rectangle RectangleObject.Width = 200 'pen size is 5 e.Graphics.DrawRectangle(New Pen(Color.Blue, 5), RectangleObject) End Sub
We can draw many different shapes and lines by using a Graphics object. You can also draw images and icons by using the ‘DrawImage()’ and ‘DrawIcon()’ methods, respectively.
Also see how you can draw Ellipse and rectangle shape on the windows form in vb.net.