-
Notifications
You must be signed in to change notification settings - Fork 0
/
NoteApplication.xaml.cs
319 lines (277 loc) · 12.1 KB
/
NoteApplication.xaml.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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
[Serializable]
public class DynamicListState
{
public string LabelText { get; set; }
public string TextBoxText { get; set; }
public bool CheckBoxChecked { get; set; }
// Add more properties as needed for your dynamic panel data
}
[Serializable]
public class WindowState
{
public List<DynamicListState> DynamicListsStates { get; set; }
public List<string> TextBoxTexts { get; set; }
public bool CheckBoxChecked { get; set; }
// Add more properties as needed for your form data
}
namespace NoteApplication
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private WindowState windowstate = new WindowState();
public MainWindow()
{
InitializeComponent();
}
private void chk_CheckAll_Click(object sender, RoutedEventArgs e)
{
foreach (var checkboxs in lst_Notes.Items)
{
if (chk_CheckAll.IsChecked == true)
{
if (checkboxs is ListBox listbox)
{
CheckBox associatedCheckBox = listbox.Items.OfType<CheckBox>().FirstOrDefault();
if (associatedCheckBox != null)
{
associatedCheckBox.IsChecked = true;
}
}
}
if (!chk_CheckAll.IsChecked == true)
{
if (checkboxs is ListBox listbox)
{
CheckBox associatedCheckBox = listbox.Items.OfType<CheckBox>().FirstOrDefault();
if (associatedCheckBox.IsChecked == true)
{
associatedCheckBox.IsChecked = false;
}
}
}
}
}
private void btn_AddPanel_Click(object sender, RoutedEventArgs e)
{
if (btn_AddPanel != null)
{
DateTime currentDate = DateTime.Now;
ListBox dynamicPanel = new ListBox();
dynamicPanel.Margin = new Thickness(1);
dynamicPanel.Visibility = Visibility.Visible;
dynamicPanel.Background = new SolidColorBrush(Colors.Brown);
CheckBox checkBox = new CheckBox();
checkBox.Visibility = Visibility.Visible;
checkBox.Margin = new Thickness(2);
TextBox textbox = new TextBox();
textbox.Name = "btn_Text";
textbox.Visibility = Visibility.Visible;
textbox.Margin = new Thickness(4);
textbox.Text = currentDate.ToString();
textbox.BorderBrush = new SolidColorBrush(Colors.White);
textbox.AcceptsReturn = true;
textbox.AcceptsTab = true;
textbox.AutoWordSelection = true;
textbox.TextAlignment = TextAlignment.Left;
textbox.TextWrapping = TextWrapping.Wrap; textbox.SelectionStart = 1;
textbox.SelectionLength = 0;
textbox.SelectionStart = 1;
textbox.Focusable = true;
textbox.UseLayoutRounding = true;
textbox.UndoLimit = 0;
textbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
textbox.Background = new SolidColorBrush(Colors.Azure);
textbox.HorizontalAlignment = HorizontalAlignment.Left;
textbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
textbox.VerticalAlignment = VerticalAlignment.Top;
textbox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
textbox.Padding = new Thickness(2);
textbox.MaxHeight = 500;
textbox.MinHeight = 200;
textbox.Width = 1340;
dynamicPanel.Items.Add(textbox);
dynamicPanel.Items.Add(checkBox);
lst_Notes.Items.Add(dynamicPanel);
}
}
private void btn_Delete_Click(object sender, RoutedEventArgs e)
{
List<ListBox> controlsToRemove = new List<ListBox>();
foreach (var item in lst_Notes.Items)
{
if (item is ListBox listbox)
{
CheckBox associatedCheckBox = listbox.Items.OfType<CheckBox>().FirstOrDefault();
if (associatedCheckBox != null && associatedCheckBox.IsChecked == true)
{
controlsToRemove.Add(listbox);
}
}
}
foreach (Control controlToRemove in controlsToRemove)
{
lst_Notes.Items.Remove(controlToRemove);
}
}
private void grd_DataGridForTexts_LayoutUpdated(object sender, EventArgs e)
{
//List<TextBox> controlsToAdd = new List<TextBox>();
//foreach (var items in lst_Notes.Items)
//{
// if (items is ListBox listbox)
// {
// CheckBox checkBox = listbox.Items.OfType<CheckBox>().FirstOrDefault();
// if (checkBox.IsChecked != null && checkBox.IsChecked == true)
// {
// TextBox textBox = new TextBox();
// textBox.Visibility = Visibility.Visible;
// textBox.Margin = new Thickness(2);
// textBox.Background = new SolidColorBrush(Colors.Black);
// // Check if a height value is non-negative before setting it
// double height = 200; // Set your desired height value
// if (height >= 0)
// {
// textBox.Height = height;
// lst_DataPanelForTexts.Items.Add(textBox);
// }
// }
// }
//}
//foreach (TextBox controlToAdd in controlsToAdd)
//{
// lst_DataPanelForTexts.Items.Add(controlToAdd);
//}
}
private void btn_Save_Click(object sender, RoutedEventArgs e)
{
Window_Save();
}
private void Window_Closing_Save(object sender, System.ComponentModel.CancelEventArgs e)
{
Window_Save();
}
private void Window_Load(object sender, RoutedEventArgs e)
{
Load_Window_State();
}
private void Window_Save()
{
try
{
// Save dynamic panel states
windowstate.DynamicListsStates = new List<DynamicListState>();
foreach (Control control in lst_Notes.Items)
{
if (control is ListBox panel)
{
DynamicListState dynamicPanelState = new DynamicListState
{
TextBoxText = panel.Items.OfType<TextBox>().FirstOrDefault()?.Text,
CheckBoxChecked = panel.Items.OfType<CheckBox>().FirstOrDefault()?.IsChecked ?? false,
//LabelText = panel.Items.OfType<Label>().FirstOrDefault().Content
};
windowstate.DynamicListsStates.Add(dynamicPanelState);
}
}
// Save to a file
string filePath = "formState.dat";
using (FileStream fileStream = new FileStream(filePath, FileMode.Create))
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(fileStream, windowstate);
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to save form state: " + ex.Message);
}
}
private void Load_Window_State()
{
try
{
// Load from a file
string filePath = "formState.dat";
if (File.Exists(filePath))
{
using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
{
IFormatter formatter = new BinaryFormatter();
windowstate = (WindowState)formatter.Deserialize(fileStream);
// Apply the loaded state to your form controls
// Recreate dynamic panels
foreach (var dynamicPanelState in windowstate.DynamicListsStates)
{
DateTime currentDate = DateTime.Now;
ListBox dynamicPanel = new ListBox();
dynamicPanel.Margin = new Thickness(1);
dynamicPanel.Visibility = Visibility.Visible;
dynamicPanel.Background = new SolidColorBrush(Colors.Brown);
CheckBox checkBox = new CheckBox();
checkBox.Visibility = Visibility.Visible;
checkBox.Margin = new Thickness(2);
TextBox textbox = new TextBox();
textbox.Name = "pnl_test";
textbox.Visibility = Visibility.Visible;
textbox.Margin = new Thickness(4);
textbox.Text = dynamicPanelState.TextBoxText; // Set the text based on the loaded state
textbox.BorderBrush = new SolidColorBrush(Colors.White);
textbox.AcceptsReturn = true;
textbox.AcceptsTab = true;
textbox.AutoWordSelection = true;
textbox.TextAlignment = TextAlignment.Left;
textbox.TextWrapping = TextWrapping.Wrap; textbox.SelectionStart = 1;
textbox.SelectionLength = 0;
textbox.SelectionStart = 1;
textbox.Focusable = true;
textbox.UseLayoutRounding = true;
textbox.UndoLimit = 0;
textbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
textbox.Background = new SolidColorBrush(Colors.Azure);
textbox.HorizontalAlignment = HorizontalAlignment.Left;
textbox.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
textbox.VerticalAlignment = VerticalAlignment.Top;
textbox.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
textbox.Padding = new Thickness(2);
textbox.MaxHeight = 500;
textbox.MinHeight = 200;
textbox.Width = 1340;
dynamicPanel.Items.Add(textbox);
dynamicPanel.Items.Add(checkBox);
lst_Notes.Items.Add(dynamicPanel);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to load form state: " + ex.Message);
}
}
private void lst_Notes_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void tabcontrol_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void btn_NavToHome_Click(object sender, RoutedEventArgs e)
{
var newWindow = new Applications.MainWindow();
newWindow.Show();
this.Close();
Window_Save();
}
}
}