How to get a Canvas Paint event to Fire?

Hello all,
I would like to switch between two images displayed in a canvas. When the user clicks on the canvas, if image A is already displayed, then that one is replaced by image B. I created a subclass of the Canvas, added return true to the mouse down event, created a ‘click’ event definition and in the mouse up event added a RaiseEvent command.

I also have a property for ImageA As Boolean.
In the Paint event I have code:

If ImageA = False Then
    g.DrawPicture(ImageA, 0, 0)
    ImageA = True
Else
    ImageA = False
    g.DrawPicture(ImageB, 0, 0)
End If

I cannot figure out how to get the paint event to fire following the initial load of the object on start up.
Can anyone shed some light on how this is done?

Thank you,
Tim

Have you tried calling Me.Invaildate after the End If?

Hi Tim,
I had not tried Invalidate before your post - thank you.
However, if I put it in the paint event after the end if, it goes into an endless loop.
If I add Invalidate to the Click Event, then it does display the second image, but it also displays the first image as well!

So how to remove or hide images so only one is displayed?

Thanks again Tim,
Tim

Oops! I wen into autopilot there and spaced on that. However, I get the update on my tests with no further interaction. Are you certain that the paint event is getting called? Are are you instantiating the image in a tight loop that may be blocking updates?

Hi Tim,
The Paint Event is definitely getting called.
This is the only code in the Paint Event

  If LandScapeSelected = False Then
      g.DrawPicture(LANDSCAP32x32, 0, 0)
  Else
     g.DrawPicture(PORTRAIT32x32, 0, 0)
  End If

This is the code in the click event:

  Me.Invalidate(False)
  If LandScapeSelected = False Then
      LandScapeSelected = True
  Else
      LandScapeSelected = False
  End If

Tim

To be clear, the image gets drawn each time. However the previous image(s) are also visible too…
So I need to clear the canvas somehow before the next image is drawn.
Tim

This fixed the problem, but not sure if it is really the correct method for doing this.
g.ClearRect(0,0, Me.Width, Me.Height)

Thanks again!
Tim

Why not just reset the canvas’ backdrop to the proper image when the user clicks?

Resetting the backdrop did nothing. Probably because there is nothing in the backdrop.

Where in Scottsdale are you? I am Ahwatukee, office in Chandler.
Tim

[quote=54567:@Tim Seyfarth]This fixed the problem, but not sure if it is really the correct method for doing this.
g.ClearRect(0,0, Me.Width, Me.Height)
[/quote]
Do you have EraseBackground set to false?

Hi Tim,
Erase background is checked.

Tim

Odd. What about DoubleBuffer? With DoubleBuffer off and EraseBackground on, you shouldn’t have any residue from the previous image.

And search your project for “DrawPicture” to make sure you’re not drawing from outside the Paint event.

Hi Tim,

The only other DrawPicture is in a BKS_ShortsViewer. No other places.
EraseBackground ON, DoubleBuffer Off.

Thank you Tim,
Tim

There are far too many Tims in this conversation :slight_smile: (not really, you can never have too many Tims).

What I was getting at was to use the BackDrop as the picture assignment instead of the Paint event. Since all that you seem to be trying to do is paint the proper decoration for that section of your controls (portrait or landscape), there’s no need to get any fancier (unless I’ve missed something).

BTW - I’m near the Fields at Talking Stick off of Via de Ventura. However, I’m in Kauai right now. It’s been raining and it’s glorious!

Oh, Duh! Your problem is here:

Me.Invalidate(False)

That means don’t erase the background before drawing the next image. Change it to just

Me.Invalidate

Sorry I missed that earlier, Tim.

(Hi, Tim)

Tim

[quote=54622:@Tim Jones]There are far too many Tims in this conversation :slight_smile: (not really, you can never have too many Tims).
[/quote]
Canadians would believe you can never have too many Tims

To get an idea of how devoted Canadians are to Tim’s they pour 77% of ALL coffee poured outside the home.
In Canada McDonald’s has 7% and Starbucks has about 3% of the market :stuck_out_tongue:
http://www.theglobeandmail.com/report-on-business/rob-magazine/how-tim-hortons-will-take-over-the-world/article580880/?page=all

Tim is right.

You’re technically disqualified from posting in this thread, but since your post was Tim-related, I fixed it for you.

Canadians would believe you can never have too many Tims

To get an idea of how devoted Canadians are to Tim’s they pour 77% of ALL coffee poured outside the home.
In Canada McDonald’s has 7% and Starbucks has about 3% of the market :stuck_out_tongue:

Another one! But “Kem” is close enough. Carry on.