Exploring the TimeCheck Application (Part 12)

Last week, in the Exploring the TimeCheck Application (Part 11) post, you discovered some of the requirements for making the Add, Edit, and Delete buttons for the Standard Project Entries and Standard Tasks lists work. This addition included creating a new dialog box so that the user could provide the information required by the lists. Now that you have the basics down, let’s look at the code for performing the task. This first bit of code performs tasks with projects.

private void btnProjAdd_Click(object sender, EventArgs e)
{
   // Create the project dialog box.
   frmProjectTask ThisProject = new frmProjectTask();
 
   // Display the project dialog box on screen.
   if (ThisProject.ShowDialog(this) == DialogResult.OK)
   {
      // Create a new project and add it to the list.
      Project NewProject = new Project();
      NewProject.Name = ThisProject.txtProject.Text;
      GroupData.ProjectList.Add(NewProject);
      GroupSettings.SaveSettings(GroupData);
   }
 
   // Update the list information.
   ClearLists();
   FillLists();
}
 
private void btnProjEdit_Click(object sender, EventArgs e)
{
   // Make sure the user has selected a value to edit.
   if (lstStandardProjects.SelectedIndex == -1)
   {
      // Display a message box telling the user to select an item.
      MessageBox.Show("You must select an item to edit.",
         "Selection Error", MessageBoxButtons.OK,
         MessageBoxIcon.Error);
 
      // Exit the event handler.
      return;
   }
 
   // Create the project dialog box.
   frmProjectTask ThisProject = new frmProjectTask("Edit a Project",
      lstStandardProjects.SelectedItem.ToString(), "&Edit");
 
   // Display the project dialog box on screen.
   if (ThisProject.ShowDialog(this) == DialogResult.OK)
   {
      // Edit the existing project name and save it to disk.
      GroupData.ProjectList[lstStandardProjects.SelectedIndex].Name
         = ThisProject.txtProject.Text;
      GroupSettings.SaveSettings(GroupData);
   }
 
   // Update the list information.
   ClearLists();
   FillLists();
}
 
private void btnProjDelete_Click(object sender, EventArgs e)
{
   // Make sure the user has selected a value to delete.
   if (lstStandardProjects.SelectedIndex == -1)
   {
      // Display a message box telling the user to select an item.
      MessageBox.Show("You must select an item to delete.",
         "Selection Error", MessageBoxButtons.OK,
         MessageBoxIcon.Error);
 
      // Exit the event handler.
      return;
   }
 
   // Remove the selected project from the list.
   GroupData.ProjectList.RemoveAt(lstStandardProjects.SelectedIndex);
   GroupSettings.SaveSettings(GroupData);
 
   // Update the list information.
   ClearLists();
   FillLists();
}

The btnProjAdd_Click() event handler begins by creating the default frmProjectTask object, ThisProject. It then displays the dialog box. When the user clicks OK, the code creates a new Project entry, NewProject, adds the project name to it from ThisProject.txtProject.Text, adds the entry to GroupData.ProjectList, and then saves GroupData. Now you know why ThisProject.txtProject.Text has to be public. There are other ways you could handle the situation, such as using a property, but this approach is faster and shouldn’t pose any security issues. You must save the changes by calling SaveSettings(). Because the list has changed, you must call ClearLists() to remove existing data from the lists and FillLists() to add the new data.

In most respects, the btnProjEdit_Click() event handler works like the btnProjAdd_Click() event handler. However, in this case, the user must choose an entry to edit, rather than create a new entry. When the user has failed to select an entry, the code displays an error message and exits. As before, the code displays the dialog box, but this time it changes the button and title bar text to reflect the difference in task. In addition, the code modifies the existing entry, rather than creating a new one. As before, you must call both ClearLists() and FillLists() to update the application.

Deleting an entry in the btnProjDelete_Click() event handler is the simplest of the three tasks. The code begins by verifying that the user has actually selected an entry and reacting if the user hasn’t. It then looks for the requested entry in the list and calls RemoveAt() to remove the entry from the list. A call to ClearLists() and FillLists() completes the process.

The three event handlers for tasks work much like those used for projects. Of course, the code makes additional changes to the frmProjectTask dialog box to ensure the user sees the right information. Here is the code for these three event handlers.

private void btnTaskAdd_Click(object sender, EventArgs e)
{
   // Create the project dialog box.
   frmProjectTask ThisTask = new frmProjectTask("Add a Task", "&Task");
 
   // Display the project dialog box on screen.
   if (ThisTask.ShowDialog(this) == DialogResult.OK)
   {
      // Create a new task and add it to the list.
      Task NewTask = new Task();
      NewTask.Name = ThisTask.txtProject.Text;
      GroupData.TaskList.Add(NewTask);
      GroupSettings.SaveSettings(GroupData);
   }
 
   // Update the list information.
   ClearLists();
   FillLists();
}
 
private void btnTaskEdit_Click(object sender, EventArgs e)
{
   // Make sure the user has selected a value to edit.
   if (lstStandardTasks.SelectedIndex == -1)
   {
      // Display a message box telling the user to select an item.
      MessageBox.Show("You must select an item to edit.",
         "Selection Error", MessageBoxButtons.OK,
         MessageBoxIcon.Error);
 
      // Exit the event handler.
      return;
   }
 
   // Create the task dialog box.
   frmProjectTask ThisTask = new frmProjectTask("Edit a Task",
      "&Task", lstStandardTasks.SelectedItem.ToString(), "&Edit");
 
   // Display the project dialog box on screen.
   if (ThisTask.ShowDialog(this) == DialogResult.OK)
   {
      // Edit the existing project name and save it to disk.
      GroupData.TaskList[lstStandardTasks.SelectedIndex].Name
         = ThisTask.txtProject.Text;
      GroupSettings.SaveSettings(GroupData);
   }
 
   // Update the list information.
   ClearLists();
   FillLists();
}
 
private void btnTaskDelete_Click(object sender, EventArgs e)
{
   // Make sure the user has selected a value to edit.
   if (lstStandardTasks.SelectedIndex == -1)
   {
      // Display a message box telling the user to select an item.
      MessageBox.Show("You must select an item to edit.",
         "Selection Error", MessageBoxButtons.OK,
         MessageBoxIcon.Error);
 
      // Exit the event handler.
      return;
   }
 
   // Remove the selected task from the list.
   GroupData.TaskList.RemoveAt(lstStandardTasks.SelectedIndex);
   GroupSettings.SaveSettings(GroupData);
 
   // Update the list information.
   ClearLists();
   FillLists();
}

As you can see, the differences aren’t all that much. Well, that’s it for this week. Please let me know if you have any questions at all about the code for these six event handlers. Next week we’ll begin looking at the code for some of the other configuration dialog box controls. In the meantime, contact me at John@JohnMuellerBooks.com with your questions and concerns. You can read the next segment in this series at Exploring the TimeCheck Application (Part 13).

 

Author: John

John Mueller is a freelance author and technical editor. He has writing in his blood, having produced 99 books and over 600 articles to date. The topics range from networking to artificial intelligence and from database management to heads-down programming. Some of his current books include a Web security book, discussions of how to manage big data using data science, a Windows command -line reference, and a book that shows how to build your own custom PC. His technical editing skills have helped over more than 67 authors refine the content of their manuscripts. John has provided technical editing services to both Data Based Advisor and Coast Compute magazines. He has also contributed articles to magazines such as Software Quality Connection, DevSource, InformIT, SQL Server Professional, Visual C++ Developer, Hard Core Visual Basic, asp.netPRO, Software Test and Performance, and Visual Basic Developer. Be sure to read John’s blog at http://blog.johnmuellerbooks.com/.

When John isn’t working at the computer, you can find him outside in the garden, cutting wood, or generally enjoying nature. John also likes making wine and knitting. When not occupied with anything else, he makes glycerin soap and candles, which comes in handy for gift baskets. You can reach John on the Internet at John@JohnMuellerBooks.com. John is also setting up a website at http://www.johnmuellerbooks.com/. Feel free to take a look and make suggestions on how he can improve it.