Sabtu, 16 Oktober 2010

Aplikasi Sederhana Count Down Timer

berikut ini aplikasi sederhana untuk count down. bisa digunakan untuk melakukan count down untuk selang waktu tertentu.

bisa didownload exe nya di link berikut ini

http://rapidshare.com/files/425356509/Count_Down_Timer.exe

aplikasi ini membutuhkan .NET Framework 4.0.

bentuk screen shot aplikasi adalah sebagai berikut:

image

untuk menggunakan aplikasi ini, gunakan tombol2 pada keyboard:

1. tombol Up Key – untuk menambah waktu count down

2. tombol Down Key – untuk mengurangi waktu count down

3. tombol Enter – untuk memulai proses count down

4. tombol Esc – untuk menghentikan proses count down

5. tombol Del – untuk reset timer count down

6 Alt + F4 – menutup aplikasi

 

dan berikut ini source code aplikasi dalam C# dan VB:

C#

using System;
using System.Drawing;
using System.Media;
using System.Windows.Forms;

namespace Count_Down_Timer
{
    public partial class FrmCountDown : Form
    {

        double lama;
        bool play;
        DateTime selesai;
        DateTime mulai;
        System.Timers.Timer timer = new System.Timers.Timer();
        System.Timers.Timer timer2 = new System.Timers.Timer();

        public FrmCountDown()
        {
            InitializeComponent();
            lama = 0;
            this.TopMost = true;
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
            this.Top = 0;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            timer2.Elapsed += new System.Timers.ElapsedEventHandler(timer2_Elapsed);
        }

        void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            this.lblTimer.Visible = !this.lblTimer.Visible;
        }

        void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (selesai == mulai)
            {
                SystemSounds.Beep.Play();
                timer2.Start();
                play = false;
                timer.Stop();
            }
            else
            {
                selesai = selesai.AddMilliseconds(-100);
                TimeSpan selisih = selesai - mulai;
                if (selisih.Minutes < 5) this.lblTimer.ForeColor = Color.Red;
                this.lblTimer.Text = string.Format("{0}:{1}.{2}",
                    selisih.Minutes.ToString("00"),
                    selisih.Seconds.ToString("00"),
                    (selisih.Milliseconds / 100).ToString("0"));
            }
        }

        private void FrmCountDown_KeyDown(object sender, KeyEventArgs e)
        {
            if (play && e.KeyCode == Keys.Escape)
            {
                play = false;
                timer.Stop();
                lama = 0;
            }
            else if (!play && e.KeyCode == Keys.Up)
            {
                if (lama < 60) lama += 1;
                this.lblTimer.Text = string.Format("{0}:00.0", lama.ToString("00"));
            }
            else if (!play && e.KeyCode == Keys.Down)
            {
                if (lama > 0) lama -= 1;
                this.lblTimer.Text = string.Format("{0}:00.0", lama.ToString("00"));
            }
            else if (!play && e.KeyCode == Keys.Delete)
            {
                timer2.Stop();
                this.lblTimer.Visible = true;
                this.lblTimer.Text = "00:00.0";
                lama = 0;
                this.lblTimer.ForeColor = Color.Blue;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                mulai = DateTime.Now;
                selesai = mulai.AddMinutes(lama);
                play = true;
                timer.Start();
            }
        }

    }
}

 

VB.NET

Public Class FrmCountDown

    Private lama As Double
    Private play As Boolean
    Private selesai As DateTime
    Private mulai As DateTime
    Private timer As New System.Timers.Timer()
    Private timer2 As New System.Timers.Timer()
   
    Public Sub New()
        InitializeComponent()
        lama = 0
        Me.TopMost = True
        Me.Left = Screen.PrimaryScreen.WorkingArea.Width - Me.Width
        Me.Top = 0
        AddHandler timer.Elapsed,
            Sub()
                If selesai = mulai Then
                    Media.SystemSounds.Beep.Play()
                    timer2.Start()
                    play = False
                    timer.Stop()
                Else
                    selesai = selesai.AddMilliseconds(-100)
                    Dim selisih As TimeSpan = selesai - mulai
                    If (selisih.Minutes < 5) Then Me.lblTimer.ForeColor = Color.Red
                    Me.Invoke(
                        Sub()
                            Me.lblTimer.Text = String.Format("{0}:{1}.{2}",
                                                 selisih.Minutes.ToString("00"),
                                                 selisih.Seconds.ToString("00"),
                                                 (selisih.Milliseconds / 100).ToString("0"))
                        End Sub, New Object() {})
                End If
            End Sub
        AddHandler timer2.Elapsed,
            Sub()
                Me.lblTimer.Visible = Not Me.lblTimer.Visible
            End Sub
    End Sub

    Private Sub FrmCountDown_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If play AndAlso e.KeyCode = Keys.Escape Then
            play = False
            timer.Stop()
            lama = 0
        ElseIf Not play AndAlso e.KeyCode = Keys.Up Then
            If lama < 60 Then lama += 1
            Me.lblTimer.Text = String.Format("{0}:00.0", lama.ToString("00"))
        ElseIf Not play AndAlso e.KeyCode = Keys.Down Then
            If lama > 0 Then lama -= 1
            Me.lblTimer.Text = String.Format("{0}:00.0", lama.ToString("00"))
        ElseIf Not play AndAlso e.KeyCode = Keys.Delete Then
            timer2.Stop()
            Me.lblTimer.Visible = True
            lama = 0
            Me.lblTimer.Text = "00:00.0"
            Me.lblTimer.ForeColor = Color.Blue
        ElseIf e.KeyCode = Keys.Enter Then
            mulai = DateTime.Now
            selesai = mulai.AddMinutes(lama)
            play = True
            timer.Start()
        End If
    End Sub

End Class

 

anda dapat mengembangkan aplikasi ini lebih lanjut lagi dan semoga bermanfaat,

 

Rgds
Adi

Tidak ada komentar: