-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAboutBoxForm.cs
175 lines (165 loc) · 5.9 KB
/
AboutBoxForm.cs
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
using System.Diagnostics;
using System.Globalization;
using ColorSelector;
using NLog;
namespace DisksizeWatcher
{
/// <summary>
/// Provides information about the culture used by the current thread
/// </summary>
internal partial class AboutBoxForm : Form
{
/// <summary>
/// Logger instance for logging messages and exceptions.
/// </summary>
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
/// Handles exceptions by logging the error and showing a message box.
/// </summary>
/// <param name="ex">The exception that occurred.</param>
/// <param name="message">The message to log and display.</param>
/// <param name="sender">The source of the event that caused the exception.</param>
/// <param name="e">The event data associated with the exception.</param>
private static void HandleException(Exception ex, string message, object? sender = null, EventArgs? e = null)
{
// Implement logging logic here (e.g., log to a file or monitoring system)
string msg = $"Error: {ex}\nMessage: {ex.Message}\nStackTrack: {ex.StackTrace}\nSender: {sender}, EventArgs: {e}";
Debug.WriteLine(value: msg);
Console.WriteLine(value: msg);
Logger.Error(exception: ex, message: msg);
_ = MessageBox.Show(text: message, caption: "Error", buttons: MessageBoxButtons.OK, icon: MessageBoxIcon.Error);
}
/// <summary>
/// Set a specific text to the status bar
/// </summary>
/// <param name="text">text with some information</param>
private void SetStatusbarText(string text)
{
try
{
labelInformation.Enabled = !string.IsNullOrEmpty(value: text);
labelInformation.Text = text;
}
catch (Exception ex)
{
HandleException(ex: ex, message: "An error occurred while setting the status bar text.");
}
}
/// <summary>
/// Constructor
/// </summary>
public AboutBoxForm()
{
try
{
InitializeComponent();
// Replace the incorrect event handler assignment with the correct method name
this.KeyDown += new KeyEventHandler(AboutBoxForm_KeyDown);
this.KeyPreview = true; // Ensures the form receives key events before the controls
Logger.Info(message: "AboutBoxForm initialized");
CultureInfo culture = CultureInfo.CurrentCulture;
Text = string.Format(provider: culture, format: "Info about {0}", args: AssemblyInfo.AssemblyTitle);
labelProductName.Text = AssemblyInfo.AssemblyProduct;
labelVersion.Text = AssemblyInfo.AssemblyVersion;
labelCompanyName.Text = AssemblyInfo.AssemblyCompany;
labelCopyright.Text = AssemblyInfo.AssemblyCopyright;
textBoxDescription.Text = AssemblyInfo.AssemblyDescription;
}
catch (Exception ex)
{
HandleException(ex: ex, message: "An error occurred while initializing the AboutBoxForm.");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="AboutBoxForm"/> class.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void AboutBoxForm_Load(object sender, EventArgs e)
{
try
{
SetStatusbarText(text: string.Empty);
}
catch (Exception ex)
{
HandleException(ex: ex, message: "An error occurred while loading the AboutBoxForm.");
}
}
/// <summary>
/// Detect the accessibility description to set as information text in the status bar
/// </summary>
/// <param name="sender">The event source.</param>
/// <param name="e">The <see cref="EventArgs"/> instance that contains the event data.</param>
private void SetStatusbar_Enter(object sender, EventArgs e)
{
try
{
if (sender is Control { AccessibleDescription: { } } control)
{
SetStatusbarText(text: control.AccessibleDescription);
}
else if (sender is ToolStripMenuItem { AccessibleDescription: { } } control2)
{
SetStatusbarText(text: control2.AccessibleDescription);
}
else if (sender is ToolStripStatusLabel { AccessibleDescription: { } } control3)
{
SetStatusbarText(text: control3.AccessibleDescription);
}
else if (sender is ToolStripButton { AccessibleDescription: { } } control4)
{
SetStatusbarText(text: control4.AccessibleDescription);
}
else if (sender is ToolStripDropDownButton { AccessibleDescription: { } } control5)
{
SetStatusbarText(text: control5.AccessibleDescription);
}
else if (sender is ToolStripSplitButton { AccessibleDescription: { } } control6)
{
SetStatusbarText(text: control6.AccessibleDescription);
}
}
catch (Exception ex)
{
HandleException(ex: ex, message: "An error occurred while setting the status bar text.", sender: sender, e: e);
}
}
/// <summary>
/// Clear the information text of the status bar
/// </summary>
/// <param name="sender">The event source.</param>
/// <param name="e">The <see cref="KeyEventArgs"/> instance that contains the event data.</param>
private void ClearStatusbar_Leave(object sender, EventArgs e)
{
try
{
SetStatusbarText(text: string.Empty);
}
catch (Exception ex)
{
HandleException(ex: ex, message: "An error occurred while clearing the status bar text.");
}
}
/// <summary>
/// Handles the KeyDown event of the LicenseForm.
/// Closes the form when the Escape key is pressed.
/// </summary>
/// <param name="sender">The event source.</param>
/// <param name="e">The <see cref="KeyEventArgs"/> instance that contains the event data.</param>
private void AboutBoxForm_KeyDown(object? sender, KeyEventArgs e)
{
try
{
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
catch (Exception ex)
{
HandleException(ex: ex, message: "An error occurred while setting the status bar text.", sender: sender, e: e);
}
}
}
}