-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
90 lines (77 loc) · 2.69 KB
/
Form1.cs
File metadata and controls
90 lines (77 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.IO;
using System.Windows.Forms;
namespace ticker
{
public partial class Form1 : Form
{
private string logFile;
public Form1()
{
InitializeComponent();
// Set up the log file path (same as in TrayApp)
string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
logFile = Path.Combine(docs, "TickerLog.csv");
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
// Check if textBox1 has content
if (string.IsNullOrWhiteSpace(textBox1.Text))
{
MessageBox.Show("Please enter a task before logging.", "Input Required", MessageBoxButtons.OK, MessageBoxIcon.Warning);
textBox1.Focus();
return;
}
// Log the task with current timestamp
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string taskText = textBox1.Text.Trim();
string logEntry = $"{timestamp},in,{taskText}";
try
{
// Append to CSV file
File.AppendAllText(logFile, logEntry + Environment.NewLine);
// Show confirmation and clear the textbox
// MessageBox.Show($"Task logged successfully!\n\nEntry: {logEntry}", "Task Logged", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Clear();
textBox1.Focus();
// close the form
this.Close();
}
catch (Exception ex)
{
MessageBox.Show($"Error writing to log file:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void Form1_Shown(object sender, EventArgs e)
{
textBox1.Focus();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// do not allow commas
if (e.KeyCode == Keys.Oemcomma)
{
e.Handled = true;
e.SuppressKeyPress = true; // Prevent comma input
return;
}
if (e.KeyCode == Keys.Enter)
{
button1.PerformClick();
e.Handled = true;
e.SuppressKeyPress = true; // Prevent ding sound
}
}
private void button2_Click(object sender, EventArgs e)
{
// close the form
this.Close();
}
}
}