Is it Recursion (or is it Memorex)

I have a function called from a paint event that writes (DrawImage) on the control.

It works ... so I thought nothing more about it ... U N T I L ...

I went back a put a breakpoint into the code to Debug a problem, and then I noticed something strange.

Instead of just working and exiting, it keep re-entering the function.

I put a counter in it and it re-entered at least 14 time.

I image it has to do with the 'Paint" event, but it certainly worried me.

Here is the really strange part: When I took the breakpoint out, the counter now says the function is only entered once.

Here is a simplified version of the code:

<Code>

Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

DrawDisplaySlip(e)

MsgBox("Count Events: " & Count)

End Sub

Sub DrawDisplaySlip(ByVal e As System.Windows.Forms.PaintEventArgs)

Dim Mark As Bitmap = My.Resources.VMark

Static CountEvents As Integer

CountEvents += 1

For Each Card In Cards

For Number = 1 To 14

Pt = oLottoMarks.GetMarkPosition(Card, Number)

e.Graphics.DrawImage(Mark, Pt)

Next

Next

Count = CountEvents ' Transfer to a global to use in MsgBox

End Sub

<\Code>

My Questions are:

1. Do I actually have recursion, or is this some strange behaviour of Debug and the breakpoint

2. If this is recursion, what is the best way to cure it (I don't want to put in a 'hokey' "Recursion Flag" if I can avoid it

Roger




Answer this question

Is it Recursion (or is it Memorex)

  • coolarian

    Hi,

    Nice code. It's looks like you understand what you're doing.

    It's possible that what you are seeing in from the debugger. To test this, I'd put a label on the main form and increment it when that pint routine is entered and often I might make the form double click clear the label.

    But overall, although I see it recommended, I don't like putting such code in a paint event, exactly because of issues like this. I know the graphic is more accessible but I create my own in other routines and may call that routine once in paint.



  • andradrr

    Thanks all ... I was hoping it was something like that.

    But, Not fair ... How can I choose the 'Best Answer" to mark when I get 3 great replies.

    I choose ReneeC since that was the first.

    A question for Renee:

    How do you get the graphic I had thought of that but I wasn't sure how to go about it.

    I remember seeing some code that grabbed the hDC, etc and used BitBlt, but I'm trying wean myself off "Old Ways".

    I also saw some code that used CreateGraphics & a new BitMap, but I can't find it now (isn't that always the way !!). I thought I might just assign the Bitmap back to e.Graphics, but, as you can tell, I'm getting in over my head now (I don't know why I have such a mental block when it comes to graphics).

    A code snippet would help.

    Thanks again all.



  • Debabrata.debroy

    Sj and DMAN are both right.

    "A question for Renee:

    How do you get the graphic I had thought of that but I wasn't sure how to go about it."

    Remember, I've always said I've always worked to get things OUT of the paint event.

    I'm not always successful. What goes in a paint event is often pretty counter intutive to me. I too use debug.writeline. Whatever works. Often when I have paint issues, it's not form wide. Very often I'm doing something to a single rectangle of screen so I usually have a restricted locale of issues and the rest of the form works as normal.



  • Oaf300092

    What you are seeing is correct. Consider what is happening:

    • Everything runs fine without the break point.
    • Add a breakpoint, your code execution stops at the breakpoint. The debugger is shown (with your breakpoint active).
    • You continue.
    • The form is reshown.
    • It needs to repaint itself: run the 'paint' event.
    • Breakpoint is hit (again)...
    • Rinse and repeat...

    As renee stated, she uses a label to indicate entry into the paint event. I use Debug.WriteLine statements (a lot!) with debug.indent and Unindent.



  • mihooper1

    I believe you have some recursion going on here....Everytime the msgbox is displayed...the paint event will be called....as well as when you call e.graphics.drawImage (14 times)...the solution is to encapsulate the drawing code completely within the paint method:



    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    Dim Mark As Bitmap = My.Resources.VMark

    For Each Card In Cards
    For Number = 1 To 14
    Pt = oLottoMarks.GetMarkPosition(Card, Number)
    e.Graphics.DrawImage(Mark, Pt)
    Next
    Next
    End Sub

     

     



  • Is it Recursion (or is it Memorex)