Solved

drawing disappears in form

Posted on 2008-10-28
4
329 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…
Suggested Courses
Course of the Month5 days, 20 hours left to enroll

626 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