Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using WhatAreYouDoingRightNow.Properties;
- namespace WhatAreYouDoingRightNow
- {
- public partial class Form1 : Form
- {
- private string _text; // text that the user will write to
- private double _timeLeft; // countdown time
- public Form1()
- {
- InitializeComponent();
- label2.Visible = true; // debug label to show timer
- this.WindowState = FormWindowState.Minimized; // start minimized in tray
- _text = ""; // reset text
- WriteLog(true); // write for the first time of today
- GetRandomTime(); // get new random time interval
- timer1.Interval = 1000; // seconds
- timer1.Start(); // start timer
- }
- // Go button (either by click or hitting Enter)
- private void HitGoButton()
- {
- _text = DateTime.Now + " - " + textBox1.Text; // append date and time
- WriteLog(false);
- _text = "";
- GetRandomTime();
- this.WindowState = FormWindowState.Minimized;
- }
- // Open txt file and write to it
- private void WriteLog(bool startOfTheDay)
- {
- // if this is the first time today --> write "START OF DAY"
- if (startOfTheDay)
- {
- string temp = _text;
- string start = "\n\n------------ START OF DAY " + DateTime.Now.Date.ToShortDateString() + " ------------\n\n";
- _text = start + temp;
- }
- // create TimeLog.txt if it doesn't exist
- if (!File.Exists("TimeLog.txt"))
- {
- using (StreamWriter writer = File.CreateText("TimeLog.txt"))
- {
- writer.WriteLine(_text);
- writer.Close();
- }
- }
- // write to the TimeLog.txt
- else
- {
- StreamWriter writer = new StreamWriter("TimeLog.txt", true, Encoding.Default);
- writer.WriteLine(_text);
- writer.Close();
- }
- textBox1.Clear();
- }
- // Tray icon (not used)
- private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
- {
- this.Show();
- this.WindowState = FormWindowState.Normal;
- }
- // Get random time between
- private void GetRandomTime()
- {
- Random rand = new Random((int) DateTime.Now.Ticks);
- _timeLeft = rand.Next(1, 2)*5; // multiplied by 60 to get minutes instead of seconds
- }
- // Count down to random time and then display the box
- private void timer1_Tick(object sender, EventArgs e)
- {
- // count down
- if (_timeLeft > 0)
- _timeLeft--;
- label2.Text = _timeLeft.ToString(); // debug label to show time
- notifyIcon1.Text = _timeLeft.ToString(); // debug show time in tray
- Console.WriteLine(_timeLeft); // debug write time to console window
- // if time ran out --> show the window
- if (_timeLeft <= 0)
- {
- this.WindowState = FormWindowState.Normal;
- }
- }
- // Hit Enter to go
- private void textBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.KeyCode == Keys.Enter)
- HitGoButton();
- }
- // Left click = go; right click = see log
- private void button1_MouseDown(object sender, MouseEventArgs e)
- {
- if (e.Button == MouseButtons.Left)
- HitGoButton();
- else if (e.Button == MouseButtons.Right)
- Process.Start("TimeLog.txt");
- }
- private void Form1_Resize_1(object sender, EventArgs e)
- {
- if (WindowState == FormWindowState.Minimized)
- {
- this.Hide(); // only show in tray icon, not in task bar
- }
- else if (WindowState == FormWindowState.Normal)
- {
- this.Show();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement