Rabu, 11 April 2012

Membuat Stopwatch Menggunakan VB.NET


Sekarang kita akan membuat aplikasi stopwatch dengan ada Start,Stop dan Selisih dalam VB.NET
nah ini langkah-langkahnya...




1. Membuat Form Baru klik Project-Add Windows Form
2. Menambahkan komponen-komponen seperti gambar


3. Mengisikan codingnya 

   Public Class Form3
    Dim Hours As Integer = 0
    Dim minutes As Integer = 0
    Dim seconds As Integer = 0
    Dim milseconds As Integer = 0
    Dim Hours1, Hours2, minutes1, minutes2, seconds1, seconds2, milseconds1, milseconds2 As Integer
    Dim mulai = False

    Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mulai = True
        CmdStart.Enabled = True 'tombol start dapat digunakan
        CmdStop.Enabled = True 'tombol stop dapat digunakan
        TxtTimer.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        TxtStart.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        TxtStop.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        TxtSelisih.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        If mulai = True Then
            'waktu berjalan
            Timer1.Start()
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        milseconds = milseconds + 1
        If milseconds = 60 Then
            milseconds = 0
            seconds = seconds + 1
        End If
        If seconds = 60 Then
            seconds = 0
            minutes = minutes + 1
        End If
        If minutes = 60 Then
            minutes = 0
            Hours = Hours + 1
        End If
        If Hours < 10 Then
            Hours = "0" + Hours
        End If
        If minutes < 10 Then
            minutes = "0" + minutes
        End If
        If seconds < 10 Then
            seconds = "0" + seconds
        End If
        If milseconds < 10 Then
            milseconds = "0" + milseconds
        End If
        TxtTimer.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
    End Sub
    Private Sub CmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdStart.Click
        'tombol start tidak dapat digunakan
        CmdStart.Enabled = False
        TxtStart.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        Hours1 = Hours
        minutes1 = minutes
        seconds1 = seconds
        milseconds1 = milseconds
    End Sub
    Private Sub CmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdStop.Click
        'tombol stop tidak dapat digunakan
        CmdStop.Enabled = False
        TxtStop.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        Hours2 = Hours
        minutes2 = minutes
        seconds2 = seconds
        milseconds2 = milseconds
        'memanggil fungsi selisih
        hitungSelisih()
    End Sub

    Private Sub CmdReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdReset.Click
        'seluruh nilai timer diulang menjadi 0
        Hours = 0
        minutes = 0
        seconds = 0
        milseconds = 0
        'tampilan seluruh label setelah direset
        TxtTimer.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        TxtStart.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        TxtStop.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        TxtSelisih.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
        mulai = False
        'tombol start dapat digunakan kembali
        CmdStart.Enabled = True
        'tombol stop dapat digunakan kembali
        CmdStop.Enabled = True
    End Sub
    'Fungsi perhitungan selisih
    Function hitungSelisih() As Integer
        If milseconds2 < milseconds1 Then
            seconds2 -= 1
            milseconds2 += 60
        End If
        If seconds2 < seconds1 Then
            minutes2 -= 1
            seconds2 += 60
        End If
        If minutes2 < minutes1 Then
            Hours2 -= 1
            minutes2 += 60
        End If
        If Hours2 < Hours1 Then
            Hours2 -= 1
        End If
        'jika Hours,minutes,seconds & milseconds tidak sama dg 0
        If Hours <> 0 Then
            Hours = Hours2 - Hours1
        End If
        If minutes <> 0 Then
            minutes = minutes2 = minutes1
        End If
        If seconds <> 0 Then
            seconds = seconds2 - seconds1
        End If
        If milseconds <> 0 Then
            milseconds = milseconds2 - milseconds1
        End If
        'menampilkan hasil selisih perhitungan waktu
     
        TxtSelisih.Text = Format(Hours, "00") & ":" & Format(minutes, "00") & ":" & Format(seconds, "00") & ":" & Format(milseconds, "00")
    End Function

End Class



4. nah stopwatch yang diinginkan telah jadi,gampang kan??hehehe


Tidak ada komentar:

Posting Komentar