martes, 21 de febrero de 2017

Clase en Visual Basic .NET que crea un cronometro conectado a una etiqueta (stopwach)

Public Class clsTimerSW
    Private m_timer As Timer
    Private m_sw As Stopwatch
    Private m_lbl As Label = Nothing

    Sub New(ByVal msInterval As Integer, Optional ByRef lbl As Label = Nothing)
        m_lbl = lbl

        m_timer = New Timer()
        m_timer.Interval = msInterval
        AddHandler m_timer.Tick, AddressOf m_timer_Tick

        m_sw = New Stopwatch()
    End Sub

    Public Sub StartTimerSW()
        If Not m_lbl Is Nothing Then m_lbl.Text = ""
        m_sw.Start()
        m_timer.Enabled = True

    End Sub

    Public Sub StopTimerSW()
        m_sw.Stop()
        m_timer.Enabled = False
    End Sub

    Public Sub ResetTimerSW()
        m_sw.Restart()
        If Not m_lbl Is Nothing Then
            m_lbl.Text = GetElapsedTime()
        End If
    End Sub

    Public Sub SetInterval(ByVal ms As Integer)
        m_timer.Interval = ms
    End Sub

    Public Function IsEnabled() As Boolean
        Return m_timer.Enabled
    End Function

    Public Function GetElapsedTime() As String
        Return String.Format("{0}:{1}:{2}", _
                m_sw.Elapsed.Hours.ToString("00"), _
                m_sw.Elapsed.Minutes.ToString("00"), _
                m_sw.Elapsed.Seconds.ToString("00"))
    End Function

    Private Sub m_timer_Tick(sender As Object, e As EventArgs)
        If Not m_lbl Is Nothing Then
            m_lbl.Text = GetElapsedTime()
            m_lbl.Refresh()
        End If
    End Sub

End Class

No hay comentarios:

Publicar un comentario

VBA Access. Redondeo de números decimales con el método medio redondeo. Alternativa a la función Round (bankers round)

 Private Function Redondeo(ByVal Numero As Variant, ByVal Decimales As Integer) As Double     'Aplica método medio redondeo (half round ...