namespace TakeNotev3.UserControls { public partial class GroupsPage : UserControl { public List groups; public GroupsPage() { InitializeComponent(); // Load the data from a file LoadData(); listView1.View = View.Tile; } private void btnAddGroup_Click(object sender, EventArgs e) { // Show a dialog box to get the name of the new group string groupName = Microsoft.VisualBasic.Interaction.InputBox("Enter the name of the new group:", "Add Group"); if (!string.IsNullOrWhiteSpace(groupName)) { // Create a new FileGroup object and add it to the groups list FileGroup newGroup = new FileGroup(groupName); groups.Add(newGroup); // Add a new ListViewGroup to the ListView control ListViewGroup listViewGroup = new ListViewGroup(groupName); listView1.Groups.Add(listViewGroup); // Save the updated data to a file SaveData(); LoadData(); } } private void btnRemoveGroup_Click(object sender, EventArgs e) { // Remove the selected group from the groups list if (listView1.SelectedItems.Count > 0) { string selectedGroupName = listView1.SelectedItems[0].Group.Header; FileGroup selectedGroup = groups.Find(g => g.Name == selectedGroupName); groups.Remove(selectedGroup); // Remove all sub items that belong to the selected group foreach (ListViewItem subItem in listView1.Items.Cast().Where(i => i.Group.Header == selectedGroupName).ToList()) { listView1.Items.Remove(subItem); } // Remove the selected group from the ListView control listView1.Groups.Remove(listView1.SelectedItems[0].Group); // Save the updated data to a file SaveData(); LoadData(); } } private void btnAddFile_Click(object sender, EventArgs e) { // Show a dialog box to let the user select one or more files OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Multiselect = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { // Add the selected files to the ListView control as sub items of the selected group ListViewGroup selectedGroup = listView1.SelectedItems.Count > 0 ? listView1.SelectedItems[0].Group : null; foreach (string fileName in openFileDialog.FileNames) { string filePath = Path.GetFullPath(fileName); ListViewItem newItem = new ListViewItem(Path.GetFileName(filePath)); newItem.Tag = filePath; listView1.Items.Add(newItem); if (selectedGroup != null) { newItem.Group = selectedGroup; FileGroup selectedFileGroup = groups.Find(g => g.Name == selectedGroup.Header); selectedFileGroup.Files.Add(filePath); } } // Save the updated data to a file SaveData(); LoadData(); } } private void btnRemoveFile_Click(object sender, EventArgs e) { // Remove the selected sub item from the ListView control and the corresponding FileGroup object if (listView1.SelectedItems.Count > 0) { ListViewItem selectedItem = listView1.SelectedItems[0]; listView1.Items.Remove(selectedItem); string selectedFilePath = (string)selectedItem.Tag; FileGroup selectedFileGroup = groups.Find(g => g.Name == selectedItem.Group.Header); selectedFileGroup.Files.Remove(selectedFilePath); // Save the updated data to a file SaveData(); LoadData(); } } private void SaveData() { // Serialize the groups list to a JSON string string data = JsonConvert.SerializeObject(groups); // Write the JSON string to a file string fileName = ""; string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string filePath = Path.Combine(appDataFolder, "TakeNotev3", "groups.json"); Directory.CreateDirectory(Path.GetDirectoryName(filePath)); } private void LoadData() { string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string filePath = Path.Combine(appDataFolder, "TakeNotev3", "groups.json"); if (File.Exists(filePath)) { string data = File.ReadAllText(filePath); // Deserialize the JSON string to a List object groups = JsonConvert.DeserializeObject>(data); // Add the FileGroup objects to the ListView control foreach (FileGroup group in groups) { ListViewGroup listViewGroup = new ListViewGroup(group.Name); listView1.Groups.Add(listViewGroup); foreach (string fileName in group.Files) { ListViewItem newItem = new ListViewItem(Path.GetFileName(fileName)); newItem.Tag = fileName; newItem.Group = listViewGroup; listView1.Items.Add(newItem); } } } else { groups = new List(); } if (listView1.Groups.Count == 0) { // If not, add a new ListViewGroup with a default name and select it ListViewGroup defaultGroup = new ListViewGroup("Default"); listView1.Groups.Add(defaultGroup); listView1.SelectedItems.Clear(); listView1.FocusedItem = null; defaultGroup.Header = "Default (Selected)"; listView1.Items.Add(new ListViewItem("No files found.", defaultGroup)); listView1.Items[0].Selected = true; } } } }