VB微波炉显示错误

时间:2013-05-12 19:46:34

标签: visual-studio-2010 vba

我正在为课堂创建一个微波炉应用程序。我有大多数应用程序工作得很好,唯一的问题是我得到一个奇怪的显示输出,我相信它与我的子字符串格式有关,但我不完全确定。基本上发生的事情是,如果用户输入说1:25烹饪时间,则输出读数为1:125,如果开始命中,则微波炉仅从1:00开始倒计时。任何帮助将不胜感激!!

Private Sub DisplayTime()
  Dim hour As Integer
  Dim second As Integer
  Dim minute As Integer

  Dim display As String ' String displays current input

  ' if too much input entered
    If timeIs.Length > 5 Then
        timeIs = timeIs.Substring(0, 5)
    End If

    display = timeIs.PadLeft(5, "0"c)

    ' extract seconds, minutes, and hours
    second = Convert.ToInt32(display.Substring(2))
    minute = Convert.ToInt32(display.Substring(1, 2))
    hour = Convert.ToInt32(display.Substring(0, 1))

    ' display number of hours, minutes, ":" seconds
    displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
    hour, minute, second)
 End Sub ' DisplayTime

' event handler displays new time each second
Private Sub clockTimer_Tick(sender As System.Object,
  e As System.EventArgs) Handles clockTimer.Tick

  ' perform countdown, subtract one second
  If timeObject.Second > 0 Then
     timeObject.Second -= 1
        displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
        timeObject.Hour, timeObject.Minute, timeObject.Second)
  ElseIf timeObject.Minute > 0 Then
     timeObject.Minute -= 1
     timeObject.Second = 59
        displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
        timeObject.Hour, timeObject.Minute, timeObject.Second)

    ElseIf timeObject.Hour > 0 Then
        timeObject.Hour -= 1
        timeObject.Minute = 59
        timeObject.Second = 59
        displayLabel.Text = String.Format("{0:D1}:{1:D2}:{2:D2}",
        timeObject.Hour, timeObject.Minute, timeObject.Second)
    Else ' countdown finished
        clockTimer.Enabled = False ' stop timer
        Beep()
        displayLabel.Text = "Done!" ' inform user time is finished
        windowPanel.BackColor = Control.DefaultBackColor
    End If
End Sub ' clockTimer_Tick
End Class ' MicrowaveOvenForm

1 个答案:

答案 0 :(得分:3)

用于提取秒部分的子字符串()是错误的。

变化:

second = Convert.ToInt32(display.Substring(2))

要:

second = Convert.ToInt32(display.Substring(3, 2))

*你需要使用“timeObject”吗?还有更好的方法来保持倒计时......