Results 1 to 13 of 13

Thread: Curious behavior when drawing.

  1. #1
    Cucumber is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    2
    Reputation
    0

    Question Curious behavior when drawing.

    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.

    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?


    Code:
    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
    ---
    The three most dangerous things in the world are a programmer with a soldering iron, a hardware engineer with a software patch, and a user with an idea.

  2. #2
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    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.
    — Stephen Paszt

  3. #3
    Cucumber is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2012
    Posts
    2
    Reputation
    0
    Thanks I'll look at that - but I suspect it might need a better understanding of .net to understand it!

    Quote Originally Posted by Paszt View Post
    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 three most dangerous things in the world are a programmer with a soldering iron, a hardware engineer with a software patch, and a user with an idea.

  4. #4
    NoIdeas is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Location
    Sweden
    Posts
    29
    Reputation
    24
    Is the form getting resized after it have been loaded? In that case, it's that the drawing surface hasn't been updated, since you defined the graphics object outside a sub. In that case, just add this sub :


    Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
    Gr = Me.CreateGraphics
    End Sub

    or you can just make sure that the graphics object is re-defined whenever the form size changes

    Hope that was helpful

    //NoIdeas

  5. #5
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    Noideas: your answer is misleading and only tells a small part of the problem. there are many other circumstances where the control would become invalid and need to be repainted. The article above explains fully.

  6. #6
    NoIdeas is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Location
    Sweden
    Posts
    29
    Reputation
    24
    Why is it misleading? I copied the code and then changed that and it worked? If it solved the problem how can it be misleading? I just tried to help by making it easier to understand, so you don't have to search through a whole article to find the solution?

    Well then, sorry for trying to help -.-
    No wonder you ain't getting no answers here when you're not allowed to answer peoples questions without any people complaining. That sure was the last time I visit this forum..

  7. #7
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    Quote Originally Posted by NoIdeas View Post
    Why is it misleading? I copied the code and then changed that and it worked? If it solved the problem how can it be misleading? I just tried to help by making it easier to understand, so you don't have to search through a whole article to find the solution?

    Well then, sorry for trying to help -.-
    No wonder you ain't getting no answers here when you're not allowed to answer peoples questions without any people complaining. That sure was the last time I visit this forum..
    I'm terribly sorry you took it as an insult. But did you stop to think that perhaps people are hesitant to answer because of responses such as yours?

  8. #8
    Paszt's Avatar
    Paszt is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Jun 2004
    Location
    Raleigh, NC - USA
    Posts
    1,502
    Reputation
    308
    I know I am.

  9. #9
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,200
    Reputation
    2369
    Quote Originally Posted by NoIdeas View Post
    Why is it misleading? I copied the code and then changed that and it worked? If it solved the problem how can it be misleading?
    Don't use the Graphics object returned by CreateGraphics for drawing, instead handle the Paint event and use the provided e.Graphics object.

  10. #10
    NoIdeas is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Location
    Sweden
    Posts
    29
    Reputation
    24
    I don't say anything like that to people who try tohelp people, but to people who complains at your answer when you're just trying to help, if you think it's a bad answer, make a better suggestion yourselves and I won't care, but if you just complain at my solution without giving an alternative solution or doesn't explain why, THATS when I get mad..

    Very sorry if we are "Taking Over" this post, and sorry if I have made someone "afraid" as Paszt said, I aprreciate if you give them better answers than me, or if you give other constructive feedback

  11. #11
    Herman is offline VB.NET Forum Miyagee
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    448
    Reputation
    346
    Often misleading answers are worse than no answer at all. Programming is supposed to be an "exact" science, in the sense that there is an exact answer for every problem. You probably answered to the best of your own understanding, I don't think anyone is blaming you for that. Just make sure that if you are unsure about something you make it clear the person reading it should probably get a second opinion.

    If this was about medicine, would you accept a so-so answer? I doubt it. The irony here is that this might well be software destined to the medical field.

  12. #12
    NoIdeas is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2011
    Location
    Sweden
    Posts
    29
    Reputation
    24
    I know that misleading answers is not good, but I still don't want to be "attacked" for making a bad answer when the person himself has'nt got any better answers. I just wanted to let the person who asked the question find a quick and working solution without reading through an article that he, as he said before, had problems understanding? So I looked through the code he gave us, made it work, and then showed him how I did. I don't get how that can be misleading..

  13. #13
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,200
    Reputation
    2369
    Quote Originally Posted by NoIdeas View Post
    I know that misleading answers is not good, but I still don't want to be "attacked" for making a bad answer when the person himself has'nt got any better answers. I just wanted to let the person who asked the question find a quick and working solution without reading through an article that he, as he said before, had problems understanding? So I looked through the code he gave us, made it work, and then showed him how I did. I don't get how that can be misleading..
    Paszt explained in post 2 that you don't use CreateGraphics for drawing, which also explains why your suggestion was such a bad one. Move on. Thread closed.

Bookmarks

Posting Permissions

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