//method called when Edit button is clicked private void btnEdit_Click(object sender, EventArgs e) { //check if any task is selected if (lvTasks.SelectedItems.Count == 0) { //if nothing is selected, show error MessageBox.Show("Select some task first", "Info", MessageBoxButtons.OK); } else { //get id of selected task - tha same way as when removing string taskId = lvTasks.SelectedItems[0].SubItems[0].Text; //find task by id TodoTask task = _todoTaskManager.FindTask(int.Parse(taskId)); //display form and pass the task TaskDetails detailsForm = new TaskDetails(task); detailsForm.ShowDialog(); //if task has been modified if (detailsForm.IsSaved) { //update tasks in the file _todoTaskManager.UpdateUserTasks(_logged.Id); //update title and is finished lvTasks.SelectedItems[0].SubItems[1].Text = task.Title; lvTasks.SelectedItems[0].SubItems[2].Text = task.IsFinished ? "✓" : "✕"; } } }
//method called when Add button is clicked private void btnAdd_Click(object sender, EventArgs e) { //determine new task id int id = _todoTaskManager.GetNextId(); //create task with new id and user id //title and description is empty yet TodoTask newTask = new TodoTask(id, _logged.Id, "", ""); //create form with task details TaskDetails detailsForm = new TaskDetails(newTask); //show form as the dialog detailsForm.ShowDialog(); //when dialog is closed check, if tash has been saved if (detailsForm.IsSaved) { //if so, add task to the list _todoTaskManager.AddTask(newTask); //and display it AddTaskToList(newTask); } }