2015-11-13 36 views
1

晚上好。VB.NET中ProgressBar特定百分比的事件

我想知道是否可以在進度條達到特定百分比時觸發事件。

例如:一旦進度條達到其值的90%,它將觸發一個消息框彈出。我使用ProgressBar比較兩次彼此。我可以在DateTimePicker中輸入一個時間,並將其與本地桌面時間進行比較。我點擊一個按鈕,將啓用定時器並開始比較這些時間。由於我比較時間,最小值和最大值總是不同,所以每次進度條達到90%時如何觸發事件?

正如你可能已經注意到,英語不是我的主要語言,所以它會更容易使用代碼來理解我現在使用:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     ProgressBar1.Minimum = 0 
     ProgressBar1.Maximum = GetTimeDifference(DateTimePicker1.Value, DateTime.Now) 
     Timer1.Start() 
    End Sub 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 
     Dim value As Integer = ProgressBar1.Maximum - GetTimeDifference(DateTimePicker1.Value, DateTime.Now) 
     If value > ProgressBar1.Maximum Then 
      Timer1.Stop() 
      Exit Sub 
     End If 
     ProgressBar1.Value = value 

    End Sub 

    Public Function GetTimeDifference(ByVal EndTime As DateTime, ByVal StartTime As DateTime) As Integer 

     Dim span As TimeSpan = EndTime.TimeOfDay - StartTime.TimeOfDay 
     Dim result As Integer = CInt(span.TotalSeconds) 

     Return result 

    End Function 

感謝, 的Jordy。

+0

這是你的設置Value屬性的代碼。所以當然你也可以添加語句來顯示消息框。十分簡單。 –

回答

1

我不認爲你需要一個事件這麼多,因爲只是一個變量來檢查,如果你打90%或不:

Private overNinety As Boolean = False 

裏面你打勾方法:

ProgressBar1.Value = value 

If Not overNinety AndAlso ProgressBar1.Value >= ProgressBar1.Maximum * 0.9 Then 
    overNinety = True 
    'Do your thing here... 
End If 
+0

工程就像一個魅力!謝謝 –