Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VB.net PaintEventArgs Handling.

Posted on 2016-11-05
10
Medium Priority
?
118 Views
Last Modified: 2016-11-09
I am working on a graphics intensive project and would like to create a class that includes a number of drawing routines. These routines could then be called from any number of places in the code. Here's an example of the type of function that I'd like to include in the class:

  Private Sub DrawEllipseGeom(
    ByVal numX As Integer,
    ByVal numY As Integer,
    ByVal numWidth As Integer,
    ByVal numHeight As Integer,
    ByVal colColor As Color,
    ByVal LineWidth As Long)

    Dim penDraw As New Pen(Drawing.Color.Black)
    Dim PenColor As Color = colColor
    penDraw.Color = PenColor
    penDraw.Width = LineWidth
 
    e.graphics.DrawEllipse(penDraw, numX, numY, numWidth, numHeight)
    Me.Invalidate()
    penDraw.Dispose()
  End Sub

The problem I'm trying to solve concerns passing e as PaintEventArgs to the functions in the class. Is there a way to do this or do I need to rethink my approach? I understand that the example above is trivial, but some of the functions will create multiple paths, perform transformations, etc. I would prefer not to have to recreate the code on each form by overriding the paint on each form.

Any ideas?

Thank you for reading my post.
0
Comment
Question by:fritz_the_blank
[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
  • 5
10 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41875945
>>The problem I'm trying to solve concerns passing e as PaintEventArgs to the functions in the class

Why is that a problem?
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 41876335
Thank you for answering. Let's say that I'd like to fire the code above from a button. The function expects e as PaintEventArgs as a parameter. I can't seem to pass that to the function via a click event.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41876383
Have a look at:
https://msdn.microsoft.com/en-us/library/5y289054(v=vs.110).aspx

I've not tried it but this might be what you want
   ' Create a Graphics object for the Control.
   Dim g As Graphics = Me.CreateGraphics()
   MyCustomDrawingRoutine(g)
  g.Dispose()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 46

Author Comment

by:fritz_the_blank
ID: 41876390
Thank you for your comment.

I'm able to do that, but there's a big difference between using .CreateGraphics and e. as PaintEventArgs. I need to be able to create paths and figures from a variety of functions and have them work with the same event handler. This allows me to create layers, manipulate them, stack them, and then finally output them in the order needed.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41876401
If you use Invalidate in the button click then a paint event will be fired.  You might have to then call your routines from the paint event after all.

However I don't understand your problem.  the PaintEventArgs contains the graphics object, the rectangle needing redrawing and nothing else for drawing.
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 41876407
I'm sorry. Let me provide some code...

I override the paint handler like this:

  Private Sub frmDrawTestPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
    Dim g As Graphics = e.Graphics
  End Sub

Now, what I'd like to do is to pass g to different subroutines or functions.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 41876636
As mentioned in an earlier comment - you pass it like you would any other parameter a function requires

  Private Sub frmDrawTestPaint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
    Dim g As Graphics = e.Graphics
MyCustomDrawingRoutine(g)
  End Sub
0
 
LVL 46

Author Comment

by:fritz_the_blank
ID: 41877536
Andy--

Thank you for staying with me! I've been away from programming for a while, and I haven't had time to study event handling in .Net as yet. So, let's say I call the routine from a form button:

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

I receive an error indicating that the mouse click event cannot be converted to a paint event. That makes sense, but I don't know how to get around that.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 41877557
An Invalidate will flag for a repaint.  As you want to run a function after the button click then something like the following (air code) is along the lines of what you want.
private bRunFunction as boolean = false

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
     bRunFunction = true
     Invalidate
  End Sub

Sub Paint(.....)
  if bRunFunction then MyCustomDrawRoutine(e.Graphics)   'or e if you wanted other things in the PaintEventArgs
end sub
0
 
LVL 46

Author Closing Comment

by:fritz_the_blank
ID: 41881178
Thank you very much. That's just what I needed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

609 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