Tasks Can Be Deleted Story

The story for tasks can be deleted was a snap.  All I had to do is work with the ListView, to delete the item upon keypress.  This only works with a selected item.

private void lvTasks_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete)
    {
        //Get the selected task
        Task task = this.SelectedTask;
        //If not null remove the task
        if (task != null)
            MGR.Tasks.Remove(task);
    }
}

Real simple to do; SelectedTask is a property that gets the selected item represented in the list view.  If not null, it is removed from the collection, which fires the TaskRemoved event, which removes the item from the list.  This is the event handler for TaskRemoved:

void Tasks_TaskRemoved(object sender, DataEventArgs<Task> e)
{
    ListViewItem item = this.FindByTag(e.Data);
    //If the item exists, remove it from the list
    if (item != null)
        this.lvTasks.Items.Remove(item);
}

FindByTag is simply an iterative method, searching the tag of all the list items in the list looking for the item.

Comments

No Comments