Suora Anne
Suora Anne

Reputation: 21

Reset a loop in Visual Basic

Basically If somebody presses button2 then I want the program to reset the measuring. The actually code keeps measuring forever and I only want to reset that measuring. Sorry for my bad english, I hope you understand what I really want, If not I will explain in details.

Here is my code:

Public Class Form1

    Private MonitorWidth As Double = 51.5 'cm 
    Private MonitorHeigth As Double = 31 'cm - This must be enter by the user
    Private TotalDistance As Double
    Private PixelHeigth As Double 'cm
    Private PixelWidth As Double 'cm
    Private WithEvents TTimer As New Timers.Timer
    Private PointQueue As New Queue(Of Point)(100)
    Private Lastpoint As Point

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        TTimer.Stop()

        While PointQueue.Count > 0
            Dim P As Point = PointQueue.Dequeue
            TotalDistance += Math.Sqrt(((Lastpoint.X - P.X) * PixelWidth) ^ 2 + ((Lastpoint.Y - P.Y) * PixelHeigth) ^ 2)
            Lastpoint = P

            TextBox1.Text = Format(TotalDistance, "Distance: #,##0.0 cm")
            TTimer.Start()

        End While

    End Sub

    Private Sub TTimer_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Handles TTimer.Elapsed
        PointQueue.Enqueue(MousePosition)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Button1.Enabled = True Then

            Lastpoint = MousePosition
            TTimer.AutoReset = True
            TTimer.Interval = 5
            TTimer.Start()
            Dim ScreenBounds As Rectangle = Screen.GetBounds(New Point(0, 0))
            Dim Screenwidth_Pixel As Integer = ScreenBounds.Width
            Dim screenHeigth_Pixel As Integer = ScreenBounds.Height
            PixelHeigth = MonitorHeigth / screenHeigth_Pixel
            PixelWidth = MonitorWidth / Screenwidth_Pixel
            Timer1.Interval = 200
            Timer1.Start()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Button2.Enabled = True Then

            Timer1.Stop()
            TextBox1.Text = ""
            TextBox1.Text = Format(TotalDistance = 0)
        End If
    End Sub
End Class

Upvotes: 2

Views: 2215

Answers (1)

Oded
Oded

Reputation: 499042

In your Button2 event handler (Button2_Click), reset the queue:

PointQueue = New Queue(Of Point)(100)

Upvotes: 1

Related Questions