03-07-2006, 07:36 PM
Studing vb.net 2003 in college class. On chapter 12 graphics and NOTHING is making any sense at all.
1. What is a graphics object and how do I create it.
2. Where do I find the paint event?
3. Can anyone explain in simple language what you need to do to draw on the form?
4 Does anyone of know of a good tutorial that covers this stuff step by step?
03-07-2006, 08:02 PM
1) The graphics has all the drawing methods. So, DrawLine, DrawEllipse etc etc are all methods of the graphics class. An instance of the graphics class is tied to the surface that you are drawing on. So all those methods will draw onto that particular surface.
There are numerous ways to get an instance of the graphics class. The cardinal #1 bold underlined rule is to avoid using CreateGraphics. It usually leads to trouble. You are (nearly?) always going to be drawing onto a control or a bitmap. If it is a bitmap then you get your graphics object from it:
Dim bm as New Bitmap(200,200)
Dim g as Graphics = bm.CreateGraphics()
If it is a control then...
2) use the controls paint event. To get to the paint event, in the code window in the Class Name combobox up at the top left of the code page, select Form1 Events. In the other box, Method Name, select Paint and Sub Paint will automagically appear.
3) Sub Paint has a paintEventArgs e, and this has a graphics object attached that is tied to the control. So If you are in the paint event for Form1, e.graphics will draw onto the form:
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawLine(Pens.Black, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height)