Electro-Team
Interesting Education Visual Basic 2010 Graphics
[email protected]
Agenda
Drawing Line
Drawing Rectangle
New Project
Windows Forms Application
Drawing Line
Add Button From Toolbox
Double Click On Button1
Private Sub Button1_Click
Dim x As Drawing.Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Brushes.Olive, 10) x.DrawLine(myPen, 10, 10, 100, 10)
End Sub
Code Illustration
Creating the Graphics Object: Dim x As Drawing.Graphics = Me.CreateGraphics Creating a Pen: Dim myPen As Pen myPen = New Pen(Brushes.Olive, 10)
Where myPen is a Pen variable. The first argument define the color of the line and the second argument define the width of the line.
x.DrawLine(myPen, 10, 10, 100, 10) Draw a line on the Form using the DrawLine method. The first argument use the Pen object , the second and third arguments define the starting coordinate the fourth and the last arguments define the ending coordinate of the line. The general syntax of the Drawline argument is : object.DrawLine(Pen, x1, y1, x2, y2)
Run Time
Challenge Yourself Try to modify Button 1 Code to get below result
Private Sub Button1_Click Dim x As Drawing.Graphics = Me.CreateGraphics Dim myPen As Pen myPen = New Pen(Brushes.Olive, 150) x.DrawLine(myPen, 10, 10, 100, 10) End Sub
Answer
Creating Rectangles
Private Sub Button1_Click
Dim x As Drawing.Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Brushes.Olive, 10)
x.DrawRectangle(myPen, 0, 0, 100, 50)
End Sub
Run Time
What is wrong .!!?
x.DrawRectangle(myPen, 0, 0, 100, 50)
Customizing Line Style
Private Sub Button1_Click
Dim x As Drawing.Graphics = Me.CreateGraphics
Dim myPen As Pen
myPen = New Pen(Drawing.Color.Red, 5)
myPen.DashStyle = Drawing.Drawing2D.DashStyle.Dash
x.DrawRectangle(myPen, 10, 10, 100, 50)
End Sub
Run Time
The End
Produced by
Electro-Team
[email protected]