Solved

drawing disappears in form

Posted on 2008-10-28
4
326 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
  • 2
  • 2
4 Comments
 
LVL 85

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 85

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

778 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