Well, thought I'd throw up a quick post about the most recent project I worked on, and actually completed. Not that it's anything fancy mind you, just something I thought would be useful. Me and RB had been discussing creating a simple app to track tasks for the last couple weeks. He actually did his version last week, and I finally sat down and dedicated 30 minutes to doing mine. I'd been keeping a list of 'chores' with pen and paper, which always seem to get misplaced or coffee split all over so it should defiantly see some use.
My basic idea was to have a container of tasks text which can be edited, updated, and deleted as needed with the ability to save the list of tasks, have them automatically loaded, and show the list so it can be worked through on a daily basis. So, after I sat down with a decent idea of the layout in mind, it didn't really take that long to finish. About the biggest thing I got hung up on was looking up references for the treeview and treenode structures. Once I had that down, the rest fell into place pretty easily. I was happy with how small/short the code is once again (Like the one page Snake game). C# does seem to be a very elegant language for dealing with certain things, and this was one that worked out well. I managed to get the whole project done in only 21 lines of code, minus of course auto-generated stuff.
Below you will find the actual code. If you want to copy and paste this, you will need to create a windows forms project. On the default form, add: a treeview control and three button controls. Button #1 should have the text changed to "Add task", Button #2 to "Clear task(s)", and Button #3 to "Exit". And of course, you'll need to space them out in whatever arrangement you find appealing. Button #2 (Clear tasks) is used once you've completed the chore and want to remove it. To make that work, you'll need to go into the treeview control and change the 'Checkboxes' property to true. You'll also want to change the LabelEdit property to true, or the user won't be able to change the text that describes the particular task at hand.
If you just want to see those magic 21 code lines in action, you can download the published executable from: https://drive.google.com/file/d/0B5moEgyck5X5TzQtT0U4MXFqNDQ/view?usp=sharing
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace Task_Tracker
{
public partial class Form1 : Form
{
string myTaskList = "Tasks.tasks";
StreamReader streamReader;
StreamWriter streamWriter;
public Form1()
{
InitializeComponent();
if (File.Exists(myTaskList))
{
streamReader = new StreamReader(myTaskList);
string textLine;
while ((textLine = streamReader.ReadLine()) != null) { treeView1.Nodes.Add(new TreeNode(textLine)); }
streamReader.Close();
}
}
private void button1_Click(object sender, EventArgs e)
{
TreeNode newNode = new TreeNode("Enter Task here");
treeView1.Nodes.Add(newNode);
}
private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
for (int currentItem = treeView1.Nodes.Count-1; currentItem >= 0; currentItem--)
{
TreeNode currentNode = treeView1.Nodes[currentItem];
if (currentNode.Checked == true) treeView1.Nodes.RemoveAt(currentItem);
}
}
private void button3_Click(object sender, EventArgs e)
{
SaveTaskList();
Environment.Exit(0);
}
private void SaveTaskList()
{
if (File.Exists(myTaskList)) { File.Delete(myTaskList); }
streamWriter = File.CreateText(myTaskList);
foreach (TreeNode currentNode in treeView1.Nodes) { streamWriter.WriteLine(currentNode.Text); }
streamWriter.Close();
}
}
}
If you're wondering how it manages to load and save the list without a lot of extraneous checking on this or that, its a simple app wide loop I worked out that goes like this: When the form is loaded, it checks to see if the default save file exists ("Tasks.tasks" ). If it does, it loads the data. It does NOT create the file if it doesn't already exist, just reads it in if it is there. When SaveTaskList gets called, it first deletes any file that is already there. (! I know, right?). SaveTaskList then creates a new file from scratch, based on the current data in the task list. You can avoid getting distracted with a ton of data editing code if you follow along with this simple layout. It may seem a little odd, but the way it works out is to always have an updated list (saved file), based on what that list contains. For this simple task, the treeview control with its text properties work out well. It's a shame that I'm just now realizing I forgot to attach the SaveTaskList method to the forms closing event. Well, if you dont hit 'Exit' the list won't get saved.
Guess I'll add that to the task list.
"May the mercy of His Divine Shadow fall upon you." - Stanley H. Tweedle, Security Guard class IV, The League of 20,000 planets