Solved

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

Posted on 2013-12-13
5
3,198 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
[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
5 Comments
 
LVL 63

Accepted Solution

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

737 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