2017-07-05 100 views
-1

我開始我的第一個步驟到Visual Basic一定量後測量加班,並試圖建立某種秒錶。Visual Basic中 - 啓動秒錶分鐘

我的設計如下:

1

背後的想法是創建支持辯論的工具。人得到了一定的時間(7分鐘)展示他們的話題,此後將有設置爲13分鐘空間互動會話(Q & A)。這個想法是,7分鐘後蜂鳴器響起,停止提交時間並轉到交互部分。 20分鐘後,第二個秒錶以蜂鳴器和紅色閃爍背景開始,指示會話需要終止。

這是我已經得到了,我很舒爾它可以被編碼,否則,也許更容易。 我已經得到了它的第一個工作日,但秒錶我沒有得到其餘的工作:

Public Class Form1 

    Private Hundredths As Integer = 0 
    Private Seconds As Integer = 0 
    Private Minutes As Integer = 0 
    Private Hours As Integer = 0 
    Private OvertimeHundredths As Integer = 0 
    Private OvertimeSeconds As Integer = 0 
    Private OvertimeMinutes As Integer = 0 
    Private OvertimeHours As Integer = 0 

    Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click 
     If Timer1.Enabled Then 
      Timer1.Stop() 
      StartBtn.Text = "START" 
      Return 
     End If 

     If Not Timer1.Enabled Then 
      Timer1.Start() 
      StartBtn.Text = "STOP" 
      Return 
     End If 
    End Sub 

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 
     Hundredths += 1 
     HundredthsTxB.Text = Hundredths.ToString 
     SecondsTxB.Text = Seconds.ToString 
     MinutesTxB.Text = Minutes.ToString 
     HoursTxB.Text = Hours.ToString 

     If Hundredths = 10 Then 
      Seconds += 1 
      Hundredths = 0 
     End If 

     If Seconds = 60 Then 
      Minutes += 1 
      Seconds = 0 
     End If 

     If Minutes = 60 Then 
      Hours += 1 
      Minutes = 0 
     End If 

     If Hours = 24 Then 
      Timer1.Stop() 
     End If 
    End Sub 

    Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click 
     Hundredths = 0 
     Seconds = 0 
     Minutes = 0 
     Hours = 0 
     HundredthsTxB.Text = Hundredths.ToString 
     SecondsTxB.Text = Seconds.ToString 
     MinutesTxB.Text = Minutes.ToString 
     HoursTxB.Text = Hours.ToString 
    End Sub 

    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick 
     OvertimeHundredths += 1 
     OvertimeHundredthsTxB.Text = OvertimeHundredths.ToString 
     OvertimeSecondsTxB.Text = OvertimeSeconds.ToString 
     OvertimeMinutesTxB.Text = OvertimeMinutes.ToString 
     OvertimeHoursTxB.Text = OvertimeHours.ToString 

     If OvertimeHundredths = 10 Then 
      OvertimeSeconds += 1 
      OvertimeHundredths = 0 
     End If 

     If OvertimeSeconds = 60 Then 
      OvertimeMinutes += 1 
      OvertimeSeconds = 0 
     End If 

     If OvertimeMinutes = 60 Then 
      OvertimeHours += 1 
      OvertimeMinutes = 0 
     End If 

     If OvertimeHours = 24 Then 
      Timer2.Stop() 
     End If 
    End Sub 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     If Timer1.Interval = 2 Then 
      Timer2.Start() 
     End If 
    End Sub 

End Class 

因此,任何幫助是值得歡迎的。此外,我熱衷於學習其他大師的思維和編碼。

+1

的問題是過於寬泛。請明確說明什麼不起作用,你嘗試了什麼。另外,考慮提出幾個問題,因爲據我所知,有幾件事情不起作用。 – Leo

+0

我首先考慮的是要拿到秒的秒錶(定時器2)填寫介紹(7)和互動的時間(13)的文本框的分鐘數後觸發。這對我的第一個版本來說已經是一個很好的發展方向。 –

+0

我建議只使用一個計時器。將其間隔設置爲小一點,如一秒鐘。每秒鐘,更新您的表格並將其添加到櫃檯。當計數器達到420時,這意味着7分鐘過去了。發出蜂鳴聲停止演示並開始交互部分。當計數器達到1200(13分鐘後,總計20分鐘)時,發出第二個蜂鳴器並開始閃爍紅光。 –

回答

0

而不是使用定時器,我會用一個Stopwatch。通過這種方式,減少了添加數字等問題。下面的代碼完成了除了發出蜂鳴聲之外的任何東西,因爲我假定您想要自己實現它。它看起來比它更糟,但似乎工作正常。見代碼註釋爲一點解釋

Public Class Form1 

    Private PresentationTimer As New Stopwatch 
    Private InteractiveTimer As New Stopwatch 
    Private PresentationMinutes As Integer 
    Private InteractiveMinutes As Integer 

    'When this timer is enabled, it keeps track of the two stopwatches and checks 
    'that they're within the alloted time. 
    ' 
    'When the presentation timer has expired, that one is stopped and the interactive 
    'timer is started 
    ' 
    'When the interactive timer has expired, the timer for background flashing starts. 
    'To stop the flashing, click the reset button on the form 
    Private Sub UiUpdateTimer_Tick(sender As Object, e As EventArgs) Handles uiUpdateTimer.Tick 
     UpdateUI() 
     If PresentationTimer.IsRunning And IsTimeExpired(PresentationTimer, PresentationMinutes) Then 
      PresentationTimer.Stop() 
      InteractiveTimer.Start() 
      'add code here for buzzer to show end of presentation time 
     End If 
     If InteractiveTimer.IsRunning And IsTimeExpired(InteractiveTimer, InteractiveMinutes) Then 
      InteractiveTimer.Stop() 
      BackgroundFlashTimer.Enabled = True 
      'add code here for buzzer to show end of interactive time 
      StartBtn.Text = "START" 
     End If 

    End Sub 

    'checks if the timer passed as a parameter is expired by comparing 
    'the number of elapsed minutes with the number of minutes passed in 
    'the second parameter 
    Private Function IsTimeExpired(tmr As Stopwatch, mins As Integer) As Boolean 
     If tmr.Elapsed.Minutes >= mins Then 
      Return True 
     Else 
      Return False 
     End If 
    End Function 

    'simply updates all the textboxes with the stopwatch values. 
    Private Sub UpdateUI() 
     HundredthsTxB.Text = Int(PresentationTimer.Elapsed.Milliseconds/10).ToString 
     SecondsTxB.Text = PresentationTimer.Elapsed.Seconds.ToString 
     MinutesTxB.Text = PresentationTimer.Elapsed.Minutes.ToString 
     HoursTxB.Text = PresentationTimer.Elapsed.Hours.ToString 
     OvertimeHundredthsTxB.Text = Int(InteractiveTimer.Elapsed.Milliseconds/10).ToString 
     OvertimeSecondsTxB.Text = InteractiveTimer.Elapsed.Seconds.ToString 
     OvertimeMinutesTxB.Text = InteractiveTimer.Elapsed.Minutes.ToString 
     overtimeHoursTxB.Text = InteractiveTimer.Elapsed.Hours.ToString 
    End Sub 

    Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click 
     'if either timer is running, stop them both and exit this sub 
     If PresentationTimer.IsRunning Or InteractiveTimer.IsRunning Then 
      PresentationTimer.Stop() 
      InteractiveTimer.Stop() 
      StartBtn.Text = "START" 
      uiUpdateTimer.Enabled = False 
      Exit Sub 
     End If 

     'if the presentation timer hasn't run yet, check if the minutes values are 
     'valid and start it 
     If PresentationTimer.ElapsedMilliseconds = 0 Then 
      PresentationMinutes = CInt(Val(PresentationMinutesTxB.Text)) 
      InteractiveMinutes = CInt(Val(InteractiveMinutesTxB.Text)) 
      'if the minutes values are't valid show messagebox and exit this sub 
      If PresentationMinutes = 0 Or InteractiveMinutes = 0 Then 
       MessageBox.Show("Please enter valid Presentation and Interactive times.") 
       Exit Sub 
      End If 
      'if minutes values are ok start the presentation timer 
      uiUpdateTimer.Enabled = True 
      PresentationTimer.Start() 
      StartBtn.Text = "STOP" 
      Exit Sub 
     End If 

     'if the presentation timer has been running, but is currently stopped, 
     'continue the timer and exit this sub 
     If Not PresentationTimer.IsRunning And PresentationTimer.ElapsedMilliseconds > 0 Then 
      uiUpdateTimer.Enabled = True 
      PresentationTimer.Start() 
      StartBtn.Text = "STOP" 
      Exit Sub 
     End If 


     'if the interactive timer has been running,but is currently stopped, 
     'continue the timer and exit this sub 
     If Not InteractiveTimer.IsRunning And InteractiveTimer.ElapsedMilliseconds > 0 Then 
      uiUpdateTimer.Enabled = True 
      InteractiveTimer.Start() 
      StartBtn.Text = "STOP" 
      Exit Sub 
     End If 

    End Sub 

    Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click 
     PresentationTimer.Reset() 
     InteractiveTimer.Reset() 
     uiUpdateTimer.Enabled = False 
     StartBtn.Enabled = True 
     StartBtn.Text = "START" 
     UpdateUI() 
     BackgroundFlashTimer.Enabled = False 
     Me.BackColor = defaultBackColor 
    End Sub 

    Private Sub BackgroundFlashTimer_Tick(sender As Object, e As EventArgs) Handles BackgroundFlashTimer.Tick 
     StartBtn.Enabled = False 
     If Me.BackColor = DefaultBackColor Then 
      Me.BackColor = Color.Red 
     Else 
      Me.BackColor = DefaultBackColor 
     End If 
     Me.Update() 
    End Sub 

End Class 
+0

哇,爲了理解你的貢獻,我不得不多次閱讀它,但是我開始看到它的邏輯。 @DavidWilson我的理解是,uiUpdateTimer&BackgroundFlashTimer還有Stopwatch屬性,並且需要它們也被預先聲明?因爲現在我收到「uiUpdateTimer/BackgroundFlashTimer未聲明,由於其保護級別,可能無法訪問。」 –

+0

對不起 - 您需要將適當命名的計時器控件添加到您的表單中。我會編輯答案:) –

+0

Thx @DavidWilson。根據您的代碼,我只是設法按照希望運行應用程序。 –

0

這是我根據大衛·威爾遜的貢獻最終的解決方案:

公共類Form1中

Private PresentationTimer As New Stopwatch 
Private InteractiveTimer As New Stopwatch 
Private ExtraTimeTimer As New Stopwatch 
Private PresentationMinutes As Integer 
Private InteractiveMinutes As Integer 

'When this timer is enabled, it keeps track of the two stopwatches and checks 
'that they're within the alloted time. 
' 
'When the presentation timer has expired, that one is stopped and the interactive 
'timer is started 
' 
'When the interactive timer has expired, the timer for background flashing starts. 
'To stop the flashing, click the reset button on the form 
Private Sub uiUpdateTimer_Tick(sender As Object, e As EventArgs) Handles uiUpdateTimer.Tick 
    UpdateUI() 
    If PresentationTimer.IsRunning And IsTimeExpired(PresentationTimer, PresentationMinutes) Then 
     PresentationTimer.Stop() 
     InteractiveTimer.Start() 
     Me.BackColor = Color.Yellow 
     'add code here for buzzer to show end of presentation time 
    End If 
    If InteractiveTimer.IsRunning And IsTimeExpired(InteractiveTimer, InteractiveMinutes) Then 
     InteractiveTimer.Stop() 
     BackgroundFlashTimer.Enabled = True 
     ExtraTimeTimer.Start() 
     'add code here for buzzer to show end of interactive time 
     StartBtn.Text = "START" 
    End If 

End Sub 

'checks if the timer passed as a parameter is expired by comparing 
'the number of elapsed minutes with the number of minutes passed in 
'the second parameter 
Private Function IsTimeExpired(tmr As Stopwatch, mins As Integer) As Boolean 
    If tmr.Elapsed.Minutes >= mins Then 
     Return True 
    Else 
     Return False 
    End If 
End Function 

'simply updates all the textboxes with the stopwatch values. 
Private Sub UpdateUI() 
    HundredthsTxB.Text = Int(PresentationTimer.Elapsed.Milliseconds/10).ToString 
    SecondsTxB.Text = PresentationTimer.Elapsed.Seconds.ToString 
    MinutesTxB.Text = PresentationTimer.Elapsed.Minutes.ToString 
    HoursTxB.Text = PresentationTimer.Elapsed.Hours.ToString 
    OvertimeHundredthsTxB.Text = Int(InteractiveTimer.Elapsed.Milliseconds/10).ToString 
    OvertimeSecondsTxB.Text = InteractiveTimer.Elapsed.Seconds.ToString 
    OvertimeMinutesTxB.Text = InteractiveTimer.Elapsed.Minutes.ToString 
    OvertimeHoursTxB.Text = InteractiveTimer.Elapsed.Hours.ToString 
    ExtraTimeHundredthsTxB.Text = Int(ExtraTimeTimer.Elapsed.Milliseconds/10).ToString 
    ExtraTimeSecondsTxB.Text = ExtraTimeTimer.Elapsed.Seconds.ToString 
    ExtraTimeMinutesTxB.Text = ExtraTimeTimer.Elapsed.Minutes.ToString 
    ExtraTimeHoursTxB.Text = ExtraTimeTimer.Elapsed.Hours.ToString 
End Sub 

Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click 
    'if either timer is running, stop them both and exit this sub 
    If PresentationTimer.IsRunning Or InteractiveTimer.IsRunning Then 
     PresentationTimer.Stop() 
     InteractiveTimer.Stop() 
     ExtraTimeTimer.Stop() 
     StartBtn.Text = "START" 
     uiUpdateTimer.Enabled = False 
     Exit Sub 
    End If 

    'if the presentation timer hasn't run yet, check if the minutes values are 
    'valid and start it 
    If PresentationTimer.ElapsedMilliseconds = 0 Then 
     PresentationMinutes = CInt(Val(PresTimeTxB.Text)) 
     InteractiveMinutes = CInt(Val(InterTimeTxB.Text)) 
     'if the minutes values are't valid show messagebox and exit this sub 
     If PresentationMinutes = 0 Or InteractiveMinutes = 0 Then 
      MessageBox.Show("Please enter valid Presentation and Interactive times.") 
      Exit Sub 
     End If 
     'if minutes values are ok start the presentation timer 
     uiUpdateTimer.Enabled = True 
     PresentationTimer.Start() 
     StartBtn.Text = "STOP" 
     Exit Sub 
    End If 

    'if the presentation timer has been running, but is currently stopped, 
    'continue the timer and exit this sub 
    If Not PresentationTimer.IsRunning And PresentationTimer.ElapsedMilliseconds > 0 Then 
     uiUpdateTimer.Enabled = True 
     PresentationTimer.Start() 
     StartBtn.Text = "STOP" 
     Exit Sub 
    End If 


    'if the interactive timer has been running,but is currently stopped, 
    'continue the timer and exit this sub 
    If Not InteractiveTimer.IsRunning And InteractiveTimer.ElapsedMilliseconds > 0 Then 
     uiUpdateTimer.Enabled = True 
     InteractiveTimer.Start() 
     StartBtn.Text = "STOP" 
     Exit Sub 
    End If 

End Sub 

Private Sub ResetBtn_Click(sender As Object, e As EventArgs) Handles ResetBtn.Click 
    PresentationTimer.Reset() 
    InteractiveTimer.Reset() 
    ExtraTimeTimer.Reset() 
    uiUpdateTimer.Enabled = False 
    StartBtn.Enabled = True 
    StartBtn.Text = "START" 
    UpdateUI() 
    BackgroundFlashTimer.Enabled = False 
    Me.BackColor = DefaultBackColor 
End Sub 

Private Sub BackgroundFlashTimer_Tick(sender As Object, e As EventArgs) Handles BackgroundFlashTimer.Tick 
    StartBtn.Enabled = False 
    If Me.BackColor = DefaultBackColor Then 
     Me.BackColor = Color.Red 
    Else 
     Me.BackColor = DefaultBackColor 
    End If 
    Me.Update() 
End Sub