-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
300 lines (282 loc) · 11.1 KB
/
MainWindow.xaml.cs
File metadata and controls
300 lines (282 loc) · 11.1 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
using DatabaseExampleWPF.Database;
using System.Windows;
namespace DatabaseExampleWPF
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// This is the "code-behind" file - it contains the C# code that handles events
/// and logic for the MainWindow.xaml user interface
///
/// This demonstrates for AQA 7517 NEA:
/// - Event handlers (responding to button clicks)
/// - Opening new windows
/// - Calling database methods
/// - User feedback with MessageBox
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Constructor - called when the window is created
/// InitializeComponent() is automatically generated and loads the XAML
/// </summary>
public MainWindow()
{
InitializeComponent();
// Check if database exists and inform user
CheckDatabaseStatus();
}
/// <summary>
/// Checks if the database exists and updates UI accordingly
/// This runs when the application starts
/// </summary>
private void CheckDatabaseStatus()
{
try
{
// Check if all required tables exist
bool allTablesExist = DatabaseHelper.TableExists("Books") &&
DatabaseHelper.TableExists("Authors") &&
DatabaseHelper.TableExists("BookAuthors") &&
DatabaseHelper.TableExists("Members") &&
DatabaseHelper.TableExists("Loans");
if (allTablesExist)
{
// Change button text to indicate database is ready
btnCreateDatabase.Content = "✓ Database Ready (Click to Recreate)";
btnCreateDatabase.Background = new System.Windows.Media.SolidColorBrush(
System.Windows.Media.Color.FromRgb(76, 175, 80)); // Green
}
else
{
// Database doesn't exist - keep default appearance
btnCreateDatabase.Content = "Create Database & Tables";
}
}
catch (Exception ex)
{
// If there's an error checking database status, log it
System.Diagnostics.Debug.WriteLine($"Error checking database status: {ex.Message}");
}
}
#region Button Event Handlers
/// <summary>
/// Event handler for Create Database button
/// Creates all database tables when clicked
///
/// Event handlers are methods that respond to user actions (like clicking a button)
/// The signature must match: void MethodName(object sender, RoutedEventArgs e)
/// - sender: The control that raised the event (the button in this case)
/// - e: Event arguments with additional information about the event
/// </summary>
private void BtnCreateDatabase_Click(object sender, RoutedEventArgs e)
{
try
{
// Call the DatabaseHelper method to create tables
bool success = DatabaseHelper.CreateTables();
if (success)
{
// MessageBox: Shows a pop-up dialog to the user
// Parameters: message text, title bar text, button options, icon
MessageBox.Show(
"Database and tables created successfully!\n\n" +
"You can now use the buttons below to manage data.\n\n" +
"Tip: You can view the database using DB Browser for SQLite.",
"Success",
MessageBoxButton.OK,
MessageBoxImage.Information);
// Update the UI to show database is ready
CheckDatabaseStatus();
}
else
{
MessageBox.Show(
"Failed to create database tables.\n\n" +
"Check the debug output for error details.",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
catch (Exception ex)
{
// Catch any unexpected errors and show them to the user
MessageBox.Show(
$"An error occurred while creating the database:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
/// <summary>
/// Opens the Books management window
/// Demonstrates how to open a new window from the main window
/// </summary>
private void BtnManageBooks_Click(object sender, RoutedEventArgs e)
{
try
{
// Create a new instance of the BooksWindow
BooksWindow booksWindow = new BooksWindow();
// Set the owner so the new window appears on top of this one
booksWindow.Owner = this;
// Show the window (non-modal - user can still interact with main window)
// Alternative: ShowDialog() would be modal - blocks interaction with main window
booksWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(
$"Error opening Books window:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
/// <summary>
/// Opens the Authors management window
/// </summary>
private void BtnManageAuthors_Click(object sender, RoutedEventArgs e)
{
try
{
AuthorsWindow authorsWindow = new AuthorsWindow();
authorsWindow.Owner = this;
authorsWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(
$"Error opening Authors window:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
/// <summary>
/// Opens the Members management window
/// </summary>
private void BtnManageMembers_Click(object sender, RoutedEventArgs e)
{
try
{
MembersWindow membersWindow = new MembersWindow();
membersWindow.Owner = this;
membersWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(
$"Error opening Members window:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
/// <summary>
/// Opens the Loans management window
/// </summary>
private void BtnManageLoans_Click(object sender, RoutedEventArgs e)
{
try
{
LoansWindow loansWindow = new LoansWindow();
loansWindow.Owner = this;
loansWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(
$"Error opening Loans window:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
/// <summary>
/// Opens the View Loans with Details window
/// This window demonstrates JOIN queries and search functionality
/// </summary>
private void BtnViewLoansWithDetails_Click(object sender, RoutedEventArgs e)
{
try
{
SearchLoansWindow searchWindow = new SearchLoansWindow();
searchWindow.Owner = this;
searchWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(
$"Error opening Search Loans window:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
/// <summary>
/// Populates the database with sample data for testing
/// </summary>
private void BtnPopulateSampleData_Click(object sender, RoutedEventArgs e)
{
try
{
// Check if database exists first
if (!DatabaseHelper.DatabaseExists())
{
MessageBox.Show(
"Please create the database first by clicking 'Create Database & Tables'.",
"Database Not Found",
MessageBoxButton.OK,
MessageBoxImage.Warning);
return;
}
// Ask for confirmation
MessageBoxResult result = MessageBox.Show(
"This will add sample data to the database:\n\n" +
"• 10 Books\n" +
"• 10 Authors\n" +
"• Book-Author relationships\n" +
"• 10 Members\n" +
"• 10 Loans (some active, some returned, some overdue)\n\n" +
"Continue?",
"Populate Sample Data",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
// Call the method to populate sample data
bool success = DatabaseHelper.PopulateSampleData();
if (success)
{
MessageBox.Show(
"Sample data added successfully!\n\n" +
"You can now explore the application with test data.\n\n" +
"Tip: Use the different management windows to view and modify the data.",
"Success",
MessageBoxButton.OK,
MessageBoxImage.Information);
}
else
{
MessageBox.Show(
"Failed to add sample data.\n\n" +
"Check the debug output for error details.",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
}
catch (Exception ex)
{
MessageBox.Show(
$"An error occurred while adding sample data:\n\n{ex.Message}",
"Error",
MessageBoxButton.OK,
MessageBoxImage.Error);
}
}
#endregion
}
}