Solved

drawing disappears in form

Posted on 2008-10-28
4
328 Views
Last Modified: 2013-11-26
hi,
I've been working on a form that is used to design a trapezoidal channel.  I have textboxes for depth, bottomwidth, sidelopes..etc.  I have a picture box that i'm using to draw the channel based on the user input.  I seem to have it working fine however at certain times the picturebox will refresh and erase my drawing with no event trigger.  For instance sometimes when tabbing out of a textbox it happens.  For a while the first time the form loaded and the drawing was triggered it would stay on the screen for approximately 2 seconds then disappear. That has since stopped but the drawing still disappears when i tab out of textboxes but not consistently.  So after reading some similar problems on here i understand that i should use the paintEventArgs.  I think i understand how to setup the sub to do what i want but there are no examples of how to call this sub routine.  i don't understand what i'm suppose to use as arguments when i call the routine that draws my drawing. my Routine.  Before i could just call my routine "DrawTrap(). I know i must be missing something simple because i've looked at dozens of examples and none of them answer this question.  Can anyone help me out?
0
Comment
Question by:ToddHawley4984
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22823381
You need to store the information about what to draw in Form level variables so they can be accessed from your Paint() event.  Populate those values and then call the Refresh() method of your PictureBox.

Here is a simplified example:
Public Class Form1
 
    Private DrawPicture As Boolean = False
    Private ptA, ptB As Point
    Private clr As Color
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ptA = New Point(25, 25)
        ptB = New Point(100, 50)
        clr = Color.Red
        DrawPicture = True
        PictureBox1.Refresh()
    End Sub
 
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        If DrawPicture Then
            Dim G As Graphics = e.Graphics
            Dim P As New Pen(clr)
            G.DrawLine(P, ptA, ptB)
            P.Dispose()
        End If
    End Sub
 
End Class

Open in new window

0
 

Author Comment

by:ToddHawley4984
ID: 22823863
i see so if you use the refresh method of a picture box the  picturebox.paint event handler is triggered.  Is that correct?  And are pens  and brushes the only thing i need to dispose of after i'm finished?  Why don't they go away like a local variable does when it exits?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22824111
That is correct...calling Refresh() triggers the Paint() event.

I can't find a good article explaining the big picture...but essentially when you use a Pen/Brush/Graphics it is tied to an underlying windows (the entire operating system) resource.  There is a limitation to these resources so you should release them back to the operating system as soon as possible by calling Dispose() on them:
http://msdn.microsoft.com/en-us/library/ms724291(VS.85).aspx

The graphics supplied in the Paint() event was NOT created by you so it does not need to be disposed manually.  If you use CreateGraphics(), however, then you should dispose of that Graphics when you are done.
0
 

Author Closing Comment

by:ToddHawley4984
ID: 31510717
Great, good info thanks so much
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question