2015-11-13 34 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

工程就像一个魅力!谢谢 –