-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
184 lines (145 loc) · 5.43 KB
/
Form1.cs
File metadata and controls
184 lines (145 loc) · 5.43 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
namespace BeepGUIApp;
using BeepGUIApp.Src;
public partial class Form1 : Form
{
private BeepConfig currentConfig;
private BeepService beepService = new BeepService();
private NumericUpDown numericMinSeconds;
private NumericUpDown numericMaxSeconds;
private NumericUpDown numericBeepLength;
private NumericUpDown numericBeepFreq;
private Label appStatusLabel;
private Label settingsSaveLabel;
private Label beepCountLabel;
private Button saveButton;
private Button startButton;
private Button stopButton;
public Form1()
{
InitializeComponent();
currentConfig = ConfigManager.Load();
this.Text = "Random Beep App";
appStatusLabel = new Label
{
Text = "Stopped!",
Left = 10,
Top = 10,
Width = 230,
ForeColor = Color.Red
};
this.Controls.Add(appStatusLabel);
// Settings boxes
Label labelMin = new Label { Text = "Min Seconds", Left = 10, Top = 40, Width = 100 };
numericMinSeconds = new NumericUpDown { Name = "numericMinSeconds", Left = 120, Top = 40, Width = 100, Minimum = 1, Maximum = 600 };
Label labelMax = new Label { Text = "Max Seconds", Left = 10, Top = 70, Width = 100 };
numericMaxSeconds = new NumericUpDown { Name = "numericMaxSeconds", Left = 120, Top = 70, Width = 100, Minimum = 1, Maximum = 600 };
Label labelLength = new Label { Text = "Beep Length (ms)", Left = 10, Top = 100, Width = 100 };
numericBeepLength = new NumericUpDown { Name = "numericBeepLength", Left = 120, Top = 100, Width = 100, Minimum = 10, Maximum = 5000 };
Label labelFreq = new Label { Text = "Beep Frequency (Hz)", Left = 10, Top = 130, Width = 100 };
numericBeepFreq = new NumericUpDown { Name = "numericBeepFreq", Left = 120, Top = 130, Width = 100, Minimum = 37, Maximum = 32767 };
this.Controls.AddRange(new Control[]
{
labelMin, numericMinSeconds,
labelMax, numericMaxSeconds,
labelLength, numericBeepLength,
labelFreq, numericBeepFreq
});
// Settings save text and button
settingsSaveLabel = new Label
{
Text = "",
Left = 10,
Top = 160,
Width = 250,
ForeColor = Color.Green,
Visible = false
};
saveButton = new Button { Text = "Save", Left = 10, Top = 185, Width = 100 };
saveButton.Click += SaveButton_Click;
this.Controls.AddRange(new Control[]
{
settingsSaveLabel, saveButton
});
// Start and stop buttons & beep count label
startButton = new Button { Text = "Start", Left = 120, Top = 185, Width = 100 };
startButton.Click += StartButton_Click;
stopButton = new Button { Text = "Stop", Left = 230, Top = 185, Width = 100, Enabled = false };
stopButton.Click += StopButton_Click;
beepCountLabel = new Label
{
Text = "Beep Count: 0",
Left = 10,
Top = 215,
Width = 230,
ForeColor = Color.Blue
};
this.Controls.AddRange([
startButton, stopButton,
beepCountLabel
]);
DisplaySettings();
}
private void DisplaySettings()
{
numericMinSeconds.Value = currentConfig.MinSeconds;
numericMaxSeconds.Value = currentConfig.MaxSeconds;
numericBeepLength.Value = currentConfig.BeepLength;
numericBeepFreq.Value = currentConfig.BeepFrequency;
}
private void SaveButton_Click(object? sender, EventArgs e)
{
currentConfig.MinSeconds = (int)numericMinSeconds.Value;
currentConfig.MaxSeconds = (int)numericMaxSeconds.Value;
currentConfig.BeepLength = (int)numericBeepLength.Value;
currentConfig.BeepFrequency = (int)numericBeepFreq.Value;
try
{
ConfigManager.Save(currentConfig);
ShowSaveText("Settings saved!", settingsSaveLabel);
}
catch (Exception ex)
{
MessageBox.Show($"Failed to save settings: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ShowSaveText(string message, Label label)
{
label.Text = message;
label.Visible = true;
// Timer to hide the label after 1.5 seconds
var timer = new System.Windows.Forms.Timer();
timer.Interval = 1500;
timer.Tick += (s, e) =>
{
label.Visible = false;
timer.Stop();
timer.Dispose();
};
timer.Start();
}
private void UpdateAppStatus(bool isRunning)
{
appStatusLabel.Text = isRunning ? "Running..." : "Stopped!";
appStatusLabel.ForeColor = isRunning ? Color.Green : Color.Red;
saveButton.Enabled = !isRunning;
startButton.Enabled = !isRunning;
stopButton.Enabled = isRunning;
}
private void StartButton_Click(object? sender, EventArgs e)
{
UpdateAppStatus(true);
beepCountLabel.Text = "Beep Count: 0";
beepService.Start(currentConfig, (count) =>
{
this.Invoke(() =>
{
beepCountLabel.Text = $"Beep Count: {count}";
});
});
}
private void StopButton_Click(object? sender, EventArgs e)
{
beepService.Stop();
UpdateAppStatus(false);
}
}