none
Capturing MouseMove Event inside a Loop RRS feed

  • Question

  • Hello;

    I have a picture box that functions as a background for the form.  When the user moves the mouse over a section of the picture, that MouseMove event triggers a loop that processes data.  everything is OK, but if the user wants to stop the calculations of the loop by moving the mouse outside the area of the picture box image nothing happens.  I am trying to figure out how to capture the mouse move event while inside a loop.

    So the generic question would be; How do you capture mouse events inside a program loop that the mouse event started?  I hope that makes sense.

    John

    Tuesday, October 30, 2018 5:23 PM

All replies

  • Hi

    Try this out and see if it fits your requirements:

    ' Form1 with PictureBox1 and Label2
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim Istoppedit As Boolean = False
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      End Sub
      Sub DoLoop()
        Static a As Double = -1111111
        Do
          a += 0.001
          Label2.Text = a.ToString("#.00")
          Application.DoEvents()
        Loop Until Istoppedit Or a > 111111
        Label2.Text = "Paused"
      End Sub
      Private Sub PictureBox1_MouseEnter(sender As Object, e As EventArgs) Handles PictureBox1.MouseEnter
        Istoppedit = False
        DoLoop()
      End Sub
      Private Sub PictureBox1_MouseLeave(sender As Object, e As EventArgs) Handles PictureBox1.MouseLeave
        Istoppedit = True
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Tuesday, October 30, 2018 5:45 PM
  • Les;

    Here is my code snippet.  I am not leaving the picture box.  In this case, I would want to stop this loop inside the mousemove event.  When I run this, once the do while ctr loop starts, I cannot stop it.

    My picturebox size is 400 by 400 and I have a label named lblstuff.

    Public Class Form1

        Dim ctr As Integer = 0

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

            If e.X > 200 Then
                'I want to stop this loop if the mouse moves to less than 200 on the x coordinate
                Do While ctr < 100000
                    lblStuff.Text = ctr
                    ctr += 1
                    lblStuff.Refresh()
                Loop
            Else
                lblStuff.Text = "STOP"
                lblStuff.Refresh()
            End If

        End Sub

    End Class

    Tuesday, October 30, 2018 6:31 PM
  • John,

    If you describe more about what it is you are doing overall it may help form a better structure...

    For example is this a game? When the mouse pointer is over the race car you want the wheels to turn...?

    It sort of sounds like you should have a continuous timing loop that is called around 30 times a second say and then your code executes in the loop based on where the mouse pointer is each time step of the loop etc.

    So the overall game loop or animation is moving forward. What you describe, when mouse over the car then make a loop... sounds backwards? But we dont know exactly what you are doing.

    :)

    Tuesday, October 30, 2018 6:33 PM
  • Les;

    Here is my code snippet.  I am not leaving the picture box.  In this case, I would want to stop this loop inside the mousemove event.  When I run this, once the do while ctr loop starts, I cannot stop it.

    My picturebox size is 400 by 400 and I have a label named lblstuff.

    Hi

    Well, from your post, where you say 'by moving the mouse outside the area of the picture box image', is what prompted me to use the Leave event.

    Actually, I wonder what is in your mind to try this, putting aloop inside the MouseMove event handler? That is just asking for trouble - think about it! Qhat happens when the User moves the mouse - jumps into the loop then what?


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, October 30, 2018 6:39 PM
    Tuesday, October 30, 2018 6:35 PM
  • Les;

    Here is my code snippet.  I am not leaving the picture box.  In this case, I would want to stop this loop inside the mousemove event.  When I run this, once the do while ctr loop starts, I cannot stop it.

    My picturebox size is 400 by 400 and I have a label named lblstuff.

    Public Class Form1

        Dim ctr As Integer = 0

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

            If e.X > 200 Then
                'I want to stop this loop if the mouse moves to less than 200 on the x coordinate
                Do While ctr < 100000
                    lblStuff.Text = ctr
                    ctr += 1
                    lblStuff.Refresh()
                Loop
            Else
                lblStuff.Text = "STOP"
                lblStuff.Refresh()
            End If

        End Sub

    End Class


    Yeah, see you should set some kind of flag variable that says if mouse is here then RacecarStage = 3.

    Then in your timing loop you if RacecarStage = 3 then Draw Wheels green.

    Then timing loop runs constantly. You draw the entire sceen each timing loop tick. Each time you draw you determine what to draw based on the current status of the things.

    It is a basic game or animation loop.

    Again, what exactly are you drawing or doing with the things?

    You can do it as Leshay is describing too but the game loop may be more what you need.

    Tuesday, October 30, 2018 6:43 PM
  • I'll try to be more clear, sorry.  We were talking about having a kind of education thing for patients here in the medical clinic.  Say you have a schematic diagram (my picture box image ) of a body.  When you mouse over the "area" that is the heart, the program could go get information/data about the heart for that patient from a database.  If they mouse over the hand, they could get pulse info for that patient, and so on.

    So each mouse over event on that image (based upon which "area" of the image they are in) would then execute some procedure/calcs/code, etc.  If they change their mind and move away from that area to another, the "code" processes would change.

    So you mouse over the heart and you start to get information on that and then they change their mind and say what was my pulse, so you move and so on. So the code executing (my loop) could change (start/stop).

    I apologize as I am not doing very well at explaining.

    john


    • Edited by Johnpilot Tuesday, October 30, 2018 7:16 PM
    Tuesday, October 30, 2018 7:15 PM