Results 1 to 3 of 3

Thread: Does VB2017 support Paint?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    87

    Does VB2017 support Paint?

    Does VN2017 Support Paint or something different.

    In VB2017 this just graphs a blank screen and it works in VB2013:
    Code:
    Public Class Form1
        Dim gphFormGraphics As Graphics = Me.CreateGraphics
        Dim pt1, pt2 As Point
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            gphFormGraphics = Me.CreateGraphics()
    
            Dim recDrawingRectangle As Rectangle = New Rectangle(0, 0, 1000, 1000)
            'Clears Graphics form
            gphFormGraphics.Clear(Me.BackColor)
            pt1 = New Point(500, 500)
            pt2 = New Point(100, 500)
            gphFormGraphics.DrawLine(Pens.Blue, pt2, pt1)
            pt1 = New Point(500, 500)
            pt2 = New Point(500, 100)
            gphFormGraphics.DrawLine(Pens.Blue, pt2, pt1)
            pt1 = New Point(500, 500)
            pt2 = New Point(100, 100)
            gphFormGraphics.DrawLine(Pens.Blue, pt2, pt1)
        End Sub
    End Class

  2. #2
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Does VB2017 support Paint?

    If it worked in a previous version, it was only by accident. There is never a reason to use CreateGraphics. It's used internally, but in terms of most code it exists to help detect bad tutorials and confuse new users today.

    There's a few reasons it's not working here, but let's go over everything at once so you understand what you need to do to fix it.

    When Windows wants to paint the window, it raises the Paint event. That event gives you a Graphics object via its PaintEventArgs, and you have a chance to draw on top of whatever else the Control drew. Since this is called every time Windows wants to redraw, what you draw in the Paint event is always there.

    When you use CreateGraphics(), you get a Graphics context on which to draw. What you draw ends up on the screen. But it's only there until Windows decides to redraw, at which point it erases the screen and raises the Paint event, then the Form/Control draws itself. The only way to use CreateGraphics() and make things stay is to handle the Paint event so you know when the form has been redrawn. But if you're handling the Paint event, you can use the Graphics object already present in the EventArgs, so there's no need to call CreateGraphics.

    This isn't working right now because the Load event does not define whether it happens before, during, or after the Form is on the screen. For this code to work, the Form has to be on the screen AND it has to be after the Paint event AND nothing after your code has to cause the screen to be redrawn. Maybe in 2013 and on certain computers all those things were true. But now, Load is happening before the form is on the screen OR before the Paint event OR before something else that raises the Paint event. So technically your code works, but it's being erased.

    So to fix it, handle the Paint event, move your code into it, replace CreateGraphics() with e.Graphics, and you'll have learned a valuable lesson: write down the name of anyone that posts a tutorial with CreateGraphics() and never take advice from them again: they failed the most basic test of "I know what I'm doing" in WinForms.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Does VB2017 support Paint?

    Interestingly, you ask whether VB 2017 supports Paint and the answer is "yes", but you're not using it. If you actually had been handling the Paint event in the first place then everything would have just worked. As Sitten Spynne says, your issue is specifically that you're using GDI+ to draw on a form while NOT handling the Paint event.

    You should not declare gphFormGraphics as a member variable at all, because it will not and should not retain a value between method calls. Get rid of that declaration, move your code to the form's Paint event handler, declare gphFormGraphics as a local variable there and assign e.Graphics to it instead of Me.CreateGraphics() and you'll be good to go.

    As an exercise, you might like to put a Debug.WriteLine or Console.WriteLine call in the Paint event handler and then watch the Output window to see how often that event is raised. You'll see that it is raised after the Load event, which is why you weren't seeing any drawing. You'll also see that it is raised if you minimise and restore the form, when you drag another window over your form and at lots of other times too. Every time that event is raised, any previous drawing will be erased, which is why you need to draw on each event.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width