Solved Paint Event - Only want to draw graphics once a button is clicked or called.

Posted on 2013-12-13
Last Modified: 2014-01-17
Here is my current code:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Public Sub DrawFault(e As PaintEventArgs, a As Integer, b As Integer)
        ' Create pen.
        Dim blackPen As New Pen(Color.Red, 2)
        ' Create location and size of ellipse.
        Dim x As Integer = 20
        Dim y As Integer = 20
        Dim width As Integer = 50
        Dim height As Integer = 50
        ' Draw ellipse to screen.
        e.Graphics.DrawEllipse(blackPen, a, b, width, height)
    End Sub
    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
        'DrawFault(e, 50, 50)
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub
End Class


It will draw the eclipse if I remove the comment before the call in Form1_paint.  But what I want to do is to draw the eclipse or call it when Button1_Click is clicked.
Question by:keith1001
LVL 62

Accepted Solution

Fernando Soto earned 500 total points
ID: 39718267
Hi keith1001;

Put a class level field and have it set to False when you want to draw the graphics set it to True to the draw and set it back to False when done.

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
    If drawGraphics Then       
        DrawFault(e, 50, 50)
    End If

End Sub

Private drawGraphics As Boolean = False

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    drawGraphics = True
    drawGraphics = False

End Sub

Open in new window

LVL 40
ID: 39718377
Fernando's solution will work, but the event will be called uselessly when you do not need to paint.

Also, the convention is to place the fields at the beginning of the class, not between methods. It makes more sense, since these variables can be used anywhere in the form. And the declarations are easier to find them if you put them all at the same place. They also do not mess up things when you collapse the code.

Instead of using a field, it would be better to simply activate and deactivate the event.

First, remove the Handles Me.Paint clause at the end of the Form1_Paint declaration. That way, the event method is not called every time that the form needs to Repaint, which can happen very often while the user is dragging or resizing things on the screen.

When the user clicks on the Button, activate the event with the following line:

AddHandler Button1.Click, AddressOf Button1_Click.

If, for some reason, you want later to deactivate the event, call RemoveHandler with the same parameters.

Author Comment

ID: 39720493
Here is my code, the eclipses do get drawn ( if you press the drawallgraphics button several times, you can see them), but they do not stay on top of the picture that is loaded into the picturebox1.

Imports System.IO
Public Class Form1
    Dim g As Graphics
    Dim failure As Graphics
    Dim LocationX(117), LocationY(117) As Integer
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint

        PictureBox1.ImageLocation = "C:\images\100704.JPG"

    End Sub
    Public Sub DrawFault(e)
        'e.Graphics.DrawEllipse(Pens.Red, 25, 25, 50, 50)
    End Sub

    Private Sub btnDrawCircle_Click(sender As Object, e As EventArgs) Handles btnDrawCircle.Click
        g = Me.CreateGraphics
        g.DrawEllipse(Pens.Red, LocationX(1), LocationY(1), 75, 75)
        'g.DrawEllipse(Pens.Red, LocationX(2), LocationY(2), 75, 75)
        'g.DrawEllipse(Pens.Red, 300, 420, 600, 580)
        ' DrawFault(e)
    End Sub

    Private Sub btnReadFile_Click(sender As Object, e As EventArgs) Handles btnReadFile.Click
        Dim x As Integer = 0
        Dim FileReader As StreamReader
        Dim filename As String
        Dim FileResultValues(117) As Integer

        filename = "c:\valuesids" & ".csv"

        If filename <> "" Then
            FileReader = New StreamReader(filename)
            Do While Not FileReader.EndOfStream
                Dim line As String = FileReader.ReadLine()
                Dim fields() As String = line.Split(","c)
                FileResultValues(x) = fields(0)
                LocationX(x) = fields(3)
                LocationY(x) = fields(4)
                ListBox1.Items.Add(FileResultValues(x) & " -  " & LocationX(x) & ":" & LocationY(x))
                x = x + 1
        End If
    End Sub

    Private Sub btnTestAllGraphics_Click(sender As Object, e As EventArgs) Handles btnTestAllGraphics.Click
        Dim x As Integer = 0
        failure = PictureBox1.CreateGraphics
        For x = 0 To 116
            failure.DrawEllipse(Pens.Red, LocationX(x), LocationY(x), 75, 75)
    End Sub
End Class
LVL 83

Expert Comment

ID: 39755019
Do you want to draw these on the picture in picturebox?

Author Comment

ID: 39776291
Yes to CodeCruiser, sorry for late repsonse, been out sick for a while.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 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

15 Experts available now in Live!

Get 1:1 Help Now