Advertisement
Guest User

WhatDoingNow

a guest
May 23rd, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using WhatAreYouDoingRightNow.Properties;
  13.  
  14. namespace WhatAreYouDoingRightNow
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         private string _text; // text that the user will write to
  19.         private double _timeLeft; // countdown time
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.  
  25.             label2.Visible = true; // debug label to show timer
  26.  
  27.             this.WindowState = FormWindowState.Minimized; // start minimized in tray
  28.  
  29.             _text = ""; // reset text
  30.             WriteLog(true); // write for the first time of today
  31.  
  32.             GetRandomTime(); // get new random time interval
  33.  
  34.             timer1.Interval = 1000; // seconds
  35.             timer1.Start(); // start timer
  36.         }
  37.  
  38.         // Go button (either by click or hitting Enter)
  39.         private void HitGoButton()
  40.         {
  41.             _text = DateTime.Now + " - " + textBox1.Text; // append date and time
  42.             WriteLog(false);
  43.  
  44.             _text = "";
  45.             GetRandomTime();
  46.             this.WindowState = FormWindowState.Minimized;
  47.         }
  48.  
  49.         // Open txt file and write to it
  50.         private void WriteLog(bool startOfTheDay)
  51.         {
  52.             // if this is the first time today --> write "START OF DAY"
  53.             if (startOfTheDay)
  54.             {
  55.                 string temp = _text;
  56.                 string start = "\n\n------------ START OF DAY " + DateTime.Now.Date.ToShortDateString() + " ------------\n\n";
  57.                 _text = start + temp;
  58.             }
  59.  
  60.             // create TimeLog.txt if it doesn't exist
  61.             if (!File.Exists("TimeLog.txt"))
  62.             {
  63.                 using (StreamWriter writer = File.CreateText("TimeLog.txt"))
  64.                 {
  65.                     writer.WriteLine(_text);
  66.                     writer.Close();
  67.                 }
  68.             }
  69.             // write to the TimeLog.txt
  70.             else
  71.             {
  72.                 StreamWriter writer = new StreamWriter("TimeLog.txt", true, Encoding.Default);
  73.                 writer.WriteLine(_text);
  74.                 writer.Close();
  75.             }
  76.  
  77.             textBox1.Clear();
  78.         }
  79.  
  80.         // Tray icon (not used)
  81.         private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
  82.         {
  83.             this.Show();
  84.             this.WindowState = FormWindowState.Normal;
  85.         }
  86.  
  87.         // Get random time between
  88.         private void GetRandomTime()
  89.         {
  90.             Random rand = new Random((int) DateTime.Now.Ticks);
  91.  
  92.             _timeLeft = rand.Next(1, 2)*5; // multiplied by 60 to get minutes instead of seconds
  93.         }
  94.  
  95.  
  96.         // Count down to random time and then display the box
  97.         private void timer1_Tick(object sender, EventArgs e)
  98.         {
  99.             // count down
  100.             if (_timeLeft > 0)
  101.                 _timeLeft--;
  102.    
  103.             label2.Text = _timeLeft.ToString(); // debug label to show time
  104.             notifyIcon1.Text = _timeLeft.ToString(); // debug show time in tray
  105.             Console.WriteLine(_timeLeft); // debug write time to console window
  106.  
  107.             // if time ran out --> show the window
  108.             if (_timeLeft <= 0)
  109.             {
  110.                 this.WindowState = FormWindowState.Normal;
  111.             }
  112.         }
  113.  
  114.         // Hit Enter to go
  115.         private void textBox1_KeyDown(object sender, KeyEventArgs e)
  116.         {
  117.             if (e.KeyCode == Keys.Enter)
  118.                 HitGoButton();
  119.         }
  120.  
  121.         // Left click = go; right click = see log
  122.         private void button1_MouseDown(object sender, MouseEventArgs e)
  123.         {
  124.             if (e.Button == MouseButtons.Left)
  125.                 HitGoButton();
  126.             else if (e.Button == MouseButtons.Right)
  127.                 Process.Start("TimeLog.txt");
  128.         }
  129.  
  130.         private void Form1_Resize_1(object sender, EventArgs e)
  131.         {
  132.             if (WindowState == FormWindowState.Minimized)
  133.             {
  134.                 this.Hide(); // only show in tray icon, not in task bar
  135.             }
  136.             else if (WindowState == FormWindowState.Normal)
  137.             {
  138.                 this.Show();
  139.             }
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement