0

I have a picturebox bigger than the form, the form have AutoScroll set to on

So to scroll the picturebox without using the scrollbars i added this code:

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove


    If e.Button = Windows.Forms.MouseButtons.Middle Then

        Dim DeltaX As Integer = (m_PanStartPoint.X - e.X)
        Dim DeltaY As Integer = (m_PanStartPoint.Y - e.Y)

        Me.AutoScrollPosition = _
        New Drawing.Point((DeltaX - Me.AutoScrollPosition.X), _
                        (DeltaY - Me.AutoScrollPosition.Y))
    End If

It works correctly if i drag and drop the picturebox for like 2 seconds, but if i hold the middle mouse button for like 5 seconds it will give me a stack overflow exeption even that its not even a loop

Is there a way to 'fix' that stack overflow? Im not sure what a Stack Overflow exactly is so im confuse about what to do.

  • mouse down event

    Private Sub PictureBox1_Mousedown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    
    m_PanStartPoint = New Point(e.X, e.Y)
    
  • A stack overflow refers the the execution stack; essentially the function calls somewhere down the line are going too deep and you're running out of space. – Casey May 20 '14 at 02:03
  • show the mousedown event, please – Ňɏssa Pøngjǣrdenlarp May 20 '14 at 02:16
  • which line exactly is the exception happening on? alone, the code posted shouldnt result in a SO. You have code elsewhere causing it. Do you have similar AutoScroll manipulations in Form events? – Ňɏssa Pøngjǣrdenlarp May 20 '14 at 03:01
  • Setting the `AutoScrollPosition` property in the `MouseMove` could trigger another `MouseMove` event, which could quickly overflow the stack. – Sam Axe May 20 '14 at 04:42
  • next time it crashes, open the Call Stack window and see where the code has been to figure out the source. – Ňɏssa Pøngjǣrdenlarp May 20 '14 at 15:21
  • the stack overflow happens at: `Me.AutoScrollPosition = _ New Drawing.Point((DeltaX - Me.AutoScrollPosition.X), _ (DeltaY - Me.AutoScrollPosition.Y))` @Plutonix –  May 20 '14 at 16:49
  • the code posted works fine (tested; cannot reproduce). Is there a timer involved in this? Examine the call stack to see how the code is getting there repeatedly – Ňɏssa Pøngjǣrdenlarp May 20 '14 at 16:52
  • @Plutonix seens that a `Application.doEvents()` much latter on the code was causing it, fixed, thanks (this question wont have an answer, should i delete it?) –  May 20 '14 at 17:26
  • never, ever use `Application.DoEvents`: http://stackoverflow.com/q/5181777/1070452 also http://blog.codinghorror.com/is-doevents-evil/ – Ňɏssa Pøngjǣrdenlarp May 20 '14 at 17:28

0 Answers0