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

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.
keith1001Asked:
Who is Participating?
 
Fernando SotoConnect With a Mentor RetiredCommented:
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
    Me.Refresh()
    drawGraphics = False

End Sub

Open in new window

0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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.
0
 
keith1001Author Commented:
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

        'DrawFault(e)
        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
            Loop
        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)
        Next
    End Sub
End Class
0
 
CodeCruiserCommented:
Do you want to draw these on the picture in picturebox?
0
 
keith1001Author Commented:
Yes to CodeCruiser, sorry for late repsonse, been out sick for a while.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.