Question Curious behavior when drawing.

Cucumber

New member
Joined
Jun 6, 2012
Messages
2
Programming Experience
10+
First of all I must say that I'm a complete newbie to VB.NET (but not to old VB6) but I've found that I need to 'update' a VB6 program (a scientific data acquisitions program) in it against the clock.:miserable:

At present I'm trying to draw crosses on a form each time the mouse is clicked. Pretty easy huh? Unfortunately while my code works it only draws crosses on the top left hand corner of the form (about up to e.Location = 300). Can anyone tell me what I am doing wrong?:worked_till_5am:


VB.NET:
Public Class Form3
    Dim Gr As Graphics = Me.CreateGraphics()


    Private Sub Form3_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        'Draw a cross
        Gr.DrawLine(Pens.Blue, e.Location.X - 5, e.Location.Y, e.Location.X + 5, e.Location.Y)
        Gr.DrawLine(Pens.Blue, e.Location.X, e.Location.Y - 5, e.Location.X, e.Location.Y + 5)
    End Sub


End Class
 
http://www.bobpowell.net is a great site for learning GDI+.

The problem you are having is explained and solved in this article: The accursed PictureBox

From the documentation of the Control.CreateGraphics Method (System.Windows.Forms) :

The Graphics object that you retrieve through the CreateGraphics method should not normally be retained after the current Windows message has been processed, because anything painted with that object will be erased with the next WM_PAINT message. Therefore you cannot cache the Graphics object for reuse, except to use non-visual methods like Graphics.MeasureString.
 

Similar threads

Back
Top