2012-11-25 34 views
2

基本上如果有人按button2然後我想程序重置測量。實際的代碼會一直測量,我只想重置該測量。對不起,我的英語不好,我希望你明白我真正想要的,如果不是,我會詳細解釋。重置VisualBasic循環

這裏是我的代碼:

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 
+2

嘗試使用,當你點擊按鈕 –

回答

1

在你Button2事件處理程序(Button2_Click),復位隊列:

PointQueue = New Queue(Of Point)(100) 
+0

我是重置一個全局變量我剛剛在button2中粘貼了這段代碼,但它不起作用,測量恢復,它不從0開始 –

+0

@SuoraAnne - 我假設你的'PointQueue'是測量值的保存位置。上面的代碼重置爲空隊列。你在哪裏粘貼它?你調試過並檢查這條線是否執行? – Oded

+0

我在Private Sub Button2_Click下粘貼了這段代碼。 –