-2
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 ShutDowner
{
    public partial class Form1 : Form
    {
        int i;
        int j;
        int k;
        int h;
        int m;
        string t;
        string ct;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Start();
            timer2.Start();

            button1.Text = "ON";


            h = DateTime.Now.Hour;//To store current hour 
            m = DateTime.Now.Minute;//To store current minute


            if (h > 12)//To make timer 12 hour
            {
                h = h - 12;
                label4.Text = h.ToString();
            }
            else if (h == 0)
            {
                h = 12;
                label4.Text = h.ToString();
            }
            else
            {
                label4.Text = h.ToString();
            }

            if (m < 10)//To print double digits for less than 10 digits eg(08)
            {
                label5.Text = "0"+m.ToString();
            }
            else
            {
                label5.Text = m.ToString();
            }

            String txt= DateTime.Now.ToShortTimeString();
            int ap = txt.IndexOf(" ");
            t = txt.Substring(ap+1);//To extract am/pm from the time
            ct = t;
            label6.Text = t;
        }





        public void button1_Click(object sender, EventArgs e)
        {

            if (button1.Text == "ON")//To turn on the timer
            {
                label3.ResetText();

                timer1.Start();

                button1.Text = "OFF";

                label5.Visible = false;
                label4.Visible = false;
                label6.Visible = false;

                button2.Visible = false;
                button3.Visible = false;
                button4.Visible = false;
                button7.Visible = false;
                button8.Visible = false;
                button9.Visible = false;

                label3.Visible = true;
                label2.Text = "Time Remaining";


                if (label6.Text == "AM")
                {

                    int sec = 60;
                    int tsec = DateTime.Now.Second;
                    int sd = sec - tsec;//second difference

                    int min = Convert.ToInt32(label5.Text);
                    int tmin = DateTime.Now.Minute;
                    int md = min - tmin;//minute difference

                    int hour = Convert.ToInt32(label4.Text);

                    int thour = DateTime.Now.Hour;
                    if (thour == 0)
                    {
                        thour = 12;
                    }
                    int hd = hour - thour;//hour difference


                    if (hour == 12 && md<0&& label6.Text == "AM"&&ct=="AM")
                    {
                        hd = hd + 24;
                    }
                    if (hour == 12 && label6.Text == "AM" && ct == "PM")
                    {
                        hd = hd-12;
                    }

                    if (hd < 0)
                    {
                        if (ct == "AM")
                        {
                            hd = hd + 12;//Make hd positive
                        }
                        else if (ct == "PM")
                        {
                            hd = hd + 24;
                        }
                        k = hd;//K is used for timer
                        if (md == 0)
                        {
                            md = 60;
                            hd = hd - 1;
                            k = hd;
                        }
                        if (md > 0)
                        {
                            k = hd;//To store hour difference in k
                        }

                    }
                    if (hd > 0)
                    {
                        k = hd - 1;

                        if (md == 0)
                        {
                            md = 60;
                            hd = hd - 1;
                            k = hd;
                        }
                        if (md > 0)
                        {
                            k = hd;
                        }
                    }
                    else if (hd == 0 && md < 0)
                    {
                        hd = hd + 24;
                        k = hd-1;
                    }
                    else
                    {
                        k = hd;
                    }


                    if (md < 0)
                    {
                        md = md + 60;
                        j = md;
                    }
                    if (md > 0)
                    {
                        md = md - 1;
                        j = md;

                        if (sd == 0)
                        {
                            sd = 60;
                        }

                    }
                    else
                    {
                        j = md;
                        if (hd == 0)
                        {
                            sd = 1;
                        }
                    }

                    i = sd;
                }

                else///Same as AM with a little difference that the system clocks gives time according to 24 hr clock so to make it 
                    ///12 hr we have used this logic.
                {
                    int sec = 60;
                    int tsec = DateTime.Now.Second;
                    int sd = sec - tsec;

                    int min = Convert.ToInt32(label5.Text);
                    int tmin = DateTime.Now.Minute;
                    int md = min - tmin;

                    int hour = Convert.ToInt32(label4.Text);
                    int thour = DateTime.Now.Hour;
                    int hd = hour - thour;

                    if (hour != 12)
                    {
                        hd = hd + 12;
                    }
                    if (hour == 12 && md<0 && label6.Text == "PM" && ct == "PM")
                    {
                        hd = hd + 24;
                    }

                   if (hd < 0)
                   {
                       hd = hd + 24;

                       k = hd;
                       if (md == 0)
                       {
                           md = 60;
                           hd = hd - 1;
                           k = hd;
                       }
                       if (md > 0)
                       {
                           k = hd;
                       }

                   }
                   if (hd > 0)
                   {
                       k = hd - 1;
                       if (md == 0)
                       {
                           md = 60;
                           hd = hd - 1;
                           k = hd;
                       }
                       if (md > 0)
                       {
                           k = hd;
                       }
                   }
                   else if (hd == 0 && md < 0)
                   {
                       hd = hd + 24;
                       k = hd-1;
                   }
                    else
                   {
                       k = hd;

                   }


                    if (md < 0)
                    {
                        md = md + 60;
                        j = md;
                    }
                    if (md > 0)
                    {
                        md = md - 1;
                        j = md;
                        if (sd == 0)
                        {
                            sd = 60;
                        }

                    }
                    else
                    {

                        j = md;
                        if (hd == 0)
                        {
                            sd = 1;
                        }
                    }

                    i = sd;

                }          




            }
            else
            {
                timer1.Stop();

                label2.Text = "Set Time";
                button1.Text = "ON";
                label5.Visible = true;
                label4.Visible = true;
                label6.Visible = true;

                button2.Visible = true;
                button3.Visible = true;
                button4.Visible = true;
                button7.Visible = true;
                button8.Visible = true;
                button9.Visible = true;



                label3.ResetText();
                label3.Visible = false;
            }


        }
        public void timer1_Tick(object sender, EventArgs e)
        {

            i--;
            if (k < 10)
            {
                if (j < 10)
                {
                    if (i < 10)
                    {
                        label3.Text = "0" + k.ToString() + ":0" + j.ToString() + ":0" + i.ToString();
                    }
                    else
                    {
                        label3.Text = "0" + k.ToString() + ":0" + j.ToString() + ":" + i.ToString();
                    }
                }
                else
                {
                    if (i < 10)
                    {
                        label3.Text = "0" + k.ToString() + ":" + j.ToString() + ":0" + i.ToString();
                    }
                    else
                    {
                        label3.Text = "0" + k.ToString() + ":" + j.ToString() + ":" + i.ToString();
                    }
                }
            }
            else
            {
                if (j < 10)
                {
                    if (i < 10)
                    {
                        label3.Text = k.ToString() + ":0" + j.ToString() + ":0" + i.ToString();
                    }
                    else
                    {
                        label3.Text = k.ToString() + ":0" + j.ToString() + ":" + i.ToString();
                    }
                }
                else
                {
                    if (i < 10)
                    {
                        label3.Text = k.ToString() + ":" + j.ToString() + ":0" + i.ToString();
                    }
                    else
                    {
                        label3.Text = k.ToString() + ":" + j.ToString() + ":" + i.ToString();
                    }
                }

            }

            if (i == 0&&j!=0)
            {
                j--;
                i = 60;//To decrease minute and set seconds again
                if(j==0&&k!=0)
                {
                    k--;//To decrease hour and set minute again
                    j = 60;

                }

            }
            if (i == 0 && j == 0 && k!=0)
            {
                j = 59;
                k--;

            }
            if (i == 0 && j == 0 && k == 0)
            {
                timer1.Stop();
                Process.Start("shutdown", "/s /t 0");//Shutdown Process
            }

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            label1.Text = DateTime.Now.ToShortTimeString();
        }

        private void button2_Click(object sender, EventArgs e)//To decrement the value of hour in set time
        {
            h--;
            label4.Text = h.ToString();
            if (h == 0)
            {
                h = 12;
                label4.Text = h.ToString();
            }
        }

        private void button3_Click(object sender, EventArgs e)//To increment the value of hour in set time
        {
            h++;
            label4.Text = h.ToString();
            if (h == 13)
            {
                h = 1;
                label4.Text = h.ToString();

            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            m--;
            if (m < 10)
            {
                label5.Text = "0" + m.ToString();
            }
            else
            {
                label5.Text = m.ToString();
            }
            if (m == -1)
            {
                h--;
                if (h == 0)
                {
                    h = 12;
                }
                label4.Text = h.ToString();

                m = 59;
                label5.Text = m.ToString();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            m++;
            if (m < 10)
            {
                label5.Text = "0" + m.ToString();
            }
            else
            {
                label5.Text = m.ToString();
            }

            if (m == 60)
            {
                h++;
                if (h == 13)
                {
                    h = 1;
                }
                label4.Text = h.ToString();

                m = 0;
                if (m < 10)
                {
                    label5.Text = "0" + m.ToString();
                }


            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (t == "AM")
            {
                t = "PM";
                label6.Text = t;
            }
            else
            {
                t = "AM";
                label6.Text = t;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (t == "AM")
            {
                t = "PM";
                label6.Text = t;
            }
            else
            {
                t = "AM";
                label6.Text = t;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;//To minimise the form
            e.Cancel = true;//To stop form closing
            notifyIcon1.Visible = true;//To make notification in system tray visible
        }

        private void returnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;//To return to normal state
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();//To close the application
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (button1.Text == "OFF")//To stop the timer if it is ON
            {
                button1_Click(stopToolStripMenuItem, e);
            }
            this.WindowState = FormWindowState.Normal;
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
        }

    }
}
2
Contributors
1
Reply
13
Views
3 Years
Discussion Span
Last Post by castajiz_2
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.