Solved

drawing disappears in form

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

763 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