Private Sub Command1_Click()
   If Command1.Caption = "开始计时(&S)" Then
       time_start = Timer                           '将当前的秒数值保存在变量time_start中
       Command1.Caption = "停止计时(&C)"
       Timer1.Enabled = True
   ElseIf Command1.Caption = "停止计时(&C)" Then
       Command1.Caption = "开始计时(&S)"
       Timer1.Enabled = False
   End If
End Sub
Private Sub Command2_Click()
   Text1.Text = ""
   Text2.Text = ""
End Sub
Private Sub Timer1_Timer()
    Text2.Text = Timer - time_start     '计算显示的时间
End SubPrivate Sub Timer2_Timer()
    Text1.Text = Now
End Sub
 在上面的代码中,如何将Text2.Text = Timer - time_start 的结果显示为“小时:分钟:秒数”的格式。
先谢谢大家!!

解决方案 »

  1.   

    看看下面的代码,注意使用 DateDiff 函数、DateAdd 函数和 Format 函数来处理Dim time_start As Date
    Private Sub Command1_Click()
      If Command1.Caption = "开始计时(&S)" Then
          time_start = Now                      '将当前的秒数值保存在变量time_start中
          Command1.Caption = "停止计时(&C)"
          Timer1.Enabled = True
      ElseIf Command1.Caption = "停止计时(&C)" Then
          Command1.Caption = "开始计时(&S)"
          Timer1.Enabled = False
      End If
    End Sub
    Private Sub Command2_Click()
      Text1.Text = ""
      Text2.Text = ""
    End SubPrivate Sub Timer1_Timer()
        Dim NowTime As Date
        NowTime = Now
        Dim TempLong As Long
        TempLong = DateDiff("s", time_start, NowTime)
        NowTime = DateAdd("s", TempLong, #1/1/1000#)
        Text2.Text = Format(NowTime, "HH:mm:ss")
    End SubPrivate Sub Timer2_Timer()
        Text1.Text = Now
    End Sub
      

  2.   

    你没有定义
    Dim time_start As Date
    在窗体代码顶部吧
    你,新建一个工程
    像上面放两个 TextBox 控件和两个 CommandBox 控件,在放一个 Timer 控件
    控件名称都采用默认值
    设置 Command1 的 Caption 为 "开始计时(&S)" 
    设置 Timer1 的 Enabled = False
    设置 Timer1 的 Interval = 1000
    然后将上面代码作为窗体程序的全部程序粘贴上去,这样绝对没问题,我这里测试正常
      

  3.   

    Dim time_start
    Private Sub Command1_Click()
      If Command1.Caption = "开始计时(&S)" Then
          time_start = Timer                          '将当前的秒数值保存在变量time_start中
          Command1.Caption = "停止计时(&C)"
          Timer1.Enabled = True
      ElseIf Command1.Caption = "停止计时(&C)" Then
          Command1.Caption = "开始计时(&S)"
          Timer1.Enabled = False
          s = Val(Text2) Mod 60
          M1 = Int(s / 60)
          m = M1 Mod 60
          H = M1 Mod 60
          T = Str(H) & ":" & Str(m) & ":" & Str(s)
           Text2.Text = Format(T, "hh:mm:ss")
      End If
    End Sub
    Private Sub Command2_Click()
      Text1.Text = ""
      Text2.Text = ""
    End Sub
    Private Sub Timer1_Timer()    Text2.Text = Timer - time_start  '计算显示的时间
    End SubPrivate Sub Timer2_Timer()
        Text1.Text = Now
    End Sub
      

  4.   

    你也可以这样改一下,Timer2就不用要了Private Sub Timer1_Timer()
        Text2.Text = Format(DateAdd("s", _
                     DateDiff("s", time_start, NowTime), #1/1/1000#), _
                     "HH:mm:ss")
        Text1.Text = Format(Now, "HH:mm:ss")
    End Sub
      

  5.   

    错了
    Private Sub Timer1_Timer()
        Text2.Text = Format(DateAdd("s", _
                     DateDiff("s", time_start, Now), #1/1/1000#), _
                     "HH:mm:ss")
        Text1.Text = Format(Now, "HH:mm:ss")
    End Sub
      

  6.   

    一个 timer 控件就够了。
    Option ExplicitPrivate time_start As Double, bStart As BooleanPrivate Sub Command1_Click()
        Timer1.Enabled = True
        bStart = Not bStart
        If (bStart) Then
            Command1.Caption = "停止计时(&C)"
            time_start = Now()
        Else
            Command1.Caption = "开始计时(&S)"
        End If
    End SubPrivate Sub Command2_Click()
        Timer1.Enabled = False
        Text1.Text = ""
        Text2.Text = ""
    End Sub
    Private Sub Form_Load()
        Timer1.Interval = 100
        bStart = False
    End SubPrivate Sub Timer1_Timer()
        Dim t As Double
        t = Now()
        Text1.Text = Format$(t, "HH:MM:SS")
        If (bStart) Then
            t = t - time_start
            Text2.Text = Format$(t, "HH:MM:SS")
        End If
    End Sub
      

  7.   


    Private Sub Timer1_Timer() 
       Dim time_end As Single, time_diff As Single   
       time_end = Timer
       time_diff = time_end - time_star
       Text2.Text = Format(TimeValue((time_diff) \ 3600 & ":" & (time_diff Mod 3600) \ 60 & ":" & time_diff Mod 3600 Mod 60), "hh:mm:ss")
    End Sub