Solved

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

Posted on 2013-12-13
5
3,268 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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