Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-12-13
5
Medium Priority
?
3,603 Views
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.
0
Comment
Question by:keith1001
5 Comments
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 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
    Me.Refresh()
    drawGraphics = False

End Sub

Open in new window

0
 
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.
0
 

Author Comment

by:keith1001
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

        '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
 
LVL 83

Expert Comment

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

Author Comment

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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

877 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