I am trying to learn how to draw/shape in vb2010. The first code example is from a book, "VB2010 Step by Step". The second and third are from MSDN.
Public Class Form1
Dim GraphicsFun As Graphics
Dim PenColor As New Pen(Color.Red)
GraphicsFun = Me.CreateGraphics
GraphicsFun.DrawLine(PenColor,20,30,100,80)
Dim pen As New Pen(Color.FromArgb(255, 0, 0, 0))
e.Graphics.DrawLine(pen, 20, 10, 300, 100)
Private Sub DrawRectangle()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)
Dim formGraphics As System.Drawing.Graphics
formGraphics = Me.CreateGraphics()
formGraphics.DrawRectangle(myPen, New Rectangle(0, 0, 200, 300))
myPen.Dispose()
formGraphics.Dispose()
End Sub
End Class
In the first two errors are "Declaration expected", GraphicsFun and e respectively.
On the third bit of code runs but I thought I would get a rectangle. I get a Blank form
Questions:
1. What am i missing, doing wrong?
2. what does "e" stand for?
3. Why do I get a blank form?
Loading
VulpesPosted Aug 31, 2013, 12:55 PM
It's difficult to know exactly what's missing but you should see a couple of lines and a rectangle drawn on your form if you change the code as below.
Note that the 'skeleton' handler for the Form1_Paint event should be added by double clicking on the Paint event in the Form's Properties Box in Visual Studio. The code can then be pasted into the skeleton handler. 'e' is the PaintEventArgs parameter to this handler.
The DrawLine method can just be inserted manually.
The Paint handler will be called automatically when the form loads but you need to call both DrawLine and DrawRectangle explicitly for them to do anything.