Solved

drawing disappears in form

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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…
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…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now