Chronomètre sans timer

Contenu du snippet

un simple chronomètre sans utiliser de timer
à la place on utilise la classe System.Diagnostics.Stopwatch
dans ce petit bout de code je démontre aussi la création et l'utilisation de contrôles créés au moment de l'éxécution :
TextBox et Cursor
pour utiliser cette source créer un nouveau projet avec un simple bouton "Button1"

Source / Exemple :


Public Class Form1

    Dim textbox1 As New TextBox

    ''' <remarks>
    ''' 
    ''' 
    ''' Dim mystopwatch As New System.Diagnostics.Stopwatch
    ''' 
    ''' utilisation du code trouvé sur :
    ''' http://www.vbfrance.com/codes/NET2-MESURER-TEMPS-EXECUTION-METHODE_33782.aspx
    ''' 
    ''' 
    ''' </remarks>
    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        textbox1 = Nothing
    End Sub

    Private Sub chrono()
        Dim mystopwatch As New System.Diagnostics.Stopwatch

        textbox1.Text = Now & vbCrLf
        mystopwatch.Start()
        MsgBox("OK to stop?", MsgBoxStyle.Information + MsgBoxStyle.SystemModal)
        textbox1.AppendText(mystopwatch.Elapsed.Days & " J-" & mystopwatch.Elapsed.Hours & " H-" & mystopwatch.Elapsed.Minutes & " M-" & mystopwatch.Elapsed.Seconds & " s-" & mystopwatch.Elapsed.Milliseconds & " m")
        mystopwatch = Nothing
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With Me
            .Text = Application.ProductName
            .Cursor = New Cursor(Application.StartupPath & "\finger.ico")
            .DesktopBounds = Rectangle.FromLTRB(10, 10, 200, 200)
            .FormBorderStyle = Windows.Forms.FormBorderStyle.SizableToolWindow
            .TopMost = False
        End With
        With textbox1
            .Parent = Me
            .Multiline = True
            .Height = (Me.Height - Me.Button1.Height) - 25
            .Width = Me.Width - 8
            .ScrollBars = ScrollBars.Both
            .WordWrap = False
            .UseWaitCursor = False
            .TabStop = True
            .Cursor = New Cursor(Application.StartupPath & "\pink256.ico")
            .Visible = True
        End With
        With Button1
            .Height = 20
            .Dock = DockStyle.Bottom
            .Cursor = Me.Cursor
            .Text = "Restart"
            .PerformClick()
        End With

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        chrono()
    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        With textbox1
            .Height = (Me.Height - Me.Button1.Height) - 25
            .Width = Me.Width - 8
        End With
    End Sub
End Class

Conclusion :


il est très facile de créer des contrôles en code et de s'assurer qu'ils auront la taille désirée à l'execution

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.