štvrtok, 24 november 2011 13:27 Written by 1791 times
Rate this item
(1 Vote)

C# - Shift Timer

 

Aplikácia, na odpočítavanie časového intervalu, napríklad pracovnej doby.

Vstupné parametre sú počiatočný čas (napríklad čas príchodu) a časový interval - čas pracovnej doby, zmeny. Aplikáciou naprogramovanou v C# sa ľahko odsleduje nadčas, prípadne koľko času ešte ostáva do konca zmeny ;)

Zdrojový kód:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace ShiftTimer
{
    public partial class FormTimer : Form
    {
        public FormTimer()
        {
            InitializeComponent();
            //set comboBox items
            for (int i = 0; i < 24; i++) 
            {
                this.comboBoxHours.Items.Insert(i, i.ToString("D2"));
            }
            for (int j = 0; j < 60; j++)
            {
                this.comboBoxMins.Items.Insert(j, j.ToString("D2"));
            }
        }

        string errorMessage = String.Empty;
        bool status = false;
        TimeSpan shiftDuration = new TimeSpan();
        DateTime startTime = new DateTime();
        DateTime endTime = new DateTime();
        DateTime actualTime = new DateTime();
        TimeSpan remainTime = new TimeSpan();
        bool endShift = false;

        private bool calculateEndTime()
        {
            try
            {
                shiftDuration = TimeSpan.Parse(this.comboBoxDuration.Text);
                startTime = DateTime.Parse(this.comboBoxHours.Text + ":" + this.comboBoxMins.Text);
                endTime = startTime + shiftDuration;
                this.textBoxEnd.Text = endTime.Hour.ToString("D2") + ":" + endTime.Minute.ToString("D2");
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return false;
            }
            return true;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            endShift = false;
            status = calculateEndTime();
            if (status != true)
            {
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            this.timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Application.DoEvents();
            actualTime = DateTime.Now;
            remainTime = actualTime - endTime;
            if (actualTime < endTime)
            {
                this.textBoxTimer.ForeColor = Color.Red;
                this.textBoxTimer.Text = "- " + remainTime.Duration().Hours.ToString("D2") + ":" + remainTime.Duration().Minutes.ToString("D2") + ":" + remainTime.Duration().Seconds.ToString("D2");
            }
            else
            {
                if (actualTime.Date == endTime.Date && actualTime.Hour == endTime.Hour && actualTime.Minute == endTime.Minute && actualTime.Second == endTime.Second && endShift == false)
                {
                    endShift = true;
                    MessageBox.Show("Your shift is over", "Shift Over", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.textBoxTimer.ForeColor = Color.Green;
                this.textBoxTimer.Text = "+ " + remainTime.Duration().Hours.ToString("D2") + ":" + remainTime.Duration().Minutes.ToString("D2") + ":" + remainTime.Duration().Seconds.ToString("D2");
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("http://www.projectik.eu");
        }
    }
}

Last modified on pondelok, 19 december 2016 21:29
Ing.Peter Šuba

Zakladateľ www.projectik.eu

It's time for another revolution

(Why so serious?)

Website: www.projectik.eu