Image

Code a Project Management Application

Welcome back! This time we will build a fully functional project management program, with tasks, deadlines, data saving and loading, and more. It’s going to be a lot of fun, so let’s dive in!


Start Visual Studio, and then create a new project. Choose the Windows Forms App – C# project template, and then press “Next”.


Let’s name our application… “Project Management”.


648c13e808e8b.png


Click “Next”. Visual Studio will give you the option to choose the target framework. Let’s play it safe and choose the default “.NET 6.0”.


Click “Create”, and Visual Studio will set up our playground, creating the code that’s absolutely needed to run our project, and then displaying an empty form.


648c13f629855.png


Let’s build the user interface! Add the following controls to the form:

- A TextBox that will allow the users to input the project name. We will name this control “textBoxProjectName”.

- Another TextBox for the new tasks. Let’s name this control “textBoxTask”.

- It’s time to add a ListBox, which will display project names, tasks and deadlines. We will call this one “listBoxTasks”.

- We also need a DateTimePicker to set task deadlines, and we will name it “dateTimePickerDeadline”.

- Finally, we will use two buttons to add and remove tasks. Their names should be “buttonAddTask” and “buttonRemoveTask”.


Here’s how my resized form looks like.


648c140344ba1.png


It’s time to write some code! My goal isn’t to offer detailed programming tutorials on this site, but to show you I’ve got the needed skills to help your business.


Here’s the entire code used for this project. This time I have used a plain text file to save and retrieve the input information. Copy/paste the code below inside form1.cs, replacing the existing code.


using System;

using System.Collections.Generic;

using System.IO;

using System.Windows.Forms;


namespace Project_Management

{

    public partial class Form1 : Form

    {

        private class Task

        {

            public string ProjectName { get; set; }

            public string Name { get; set; }

            public DateTime Deadline { get; set; }


            public override string ToString()

            {

                return $"{ProjectName} - {Name} - {Deadline.ToShortDateString()}";

            }

        }


        private List<Task> tasks;

        private string dataFilePath = "data.txt";


        public Form1()

        {

            InitializeComponent();

            tasks = new List<Task>();

            LoadData();

        }


        private void buttonAddTask_Click(object sender, EventArgs e)

        {

            string taskName = textBoxTask.Text;

            DateTime taskDeadline = dateTimePickerDeadline.Value;


            if (!string.IsNullOrEmpty(taskName))

            {

                Task newTask = new Task { ProjectName = textBoxProjectName.Text, Name = taskName, Deadline = taskDeadline };

                tasks.Add(newTask);

                listBoxTasks.Items.Add(newTask.ToString());

                textBoxTask.Text = string.Empty;

                textBoxProjectName.Text = string.Empty; // Reset project name


                SaveData();

            }

        }


        private void buttonRemoveTask_Click(object sender, EventArgs e)

        {

            if (listBoxTasks.SelectedIndex != -1)

            {

                tasks.RemoveAt(listBoxTasks.SelectedIndex);

                listBoxTasks.Items.RemoveAt(listBoxTasks.SelectedIndex);


                SaveData();

            }

        }


        private void LoadData()

        {

            if (File.Exists(dataFilePath))

            {

                try

                {

                    using (StreamReader reader = new StreamReader(dataFilePath))

                    {

                        string line;

                        while ((line = reader.ReadLine()) != null)

                        {

                            string[] parts = line.Split('|');

                            if (parts.Length == 3)

                            {

                                Task task = new Task

                                {

                                    ProjectName = parts[0],

                                    Name = parts[1],

                                    Deadline = DateTime.Parse(parts[2])

                                };

                                tasks.Add(task);

                                listBoxTasks.Items.Add(task.ToString());

                            }

                        }

                    }

                }

                catch (Exception ex)

                {

                    MessageBox.Show("Error while loading data: " + ex.Message);

                }

            }

        }


        private void SaveData()

        {

            try

            {

                using (StreamWriter writer = new StreamWriter(dataFilePath))

                {

                    foreach (Task task in tasks)

                    {

                        string line = $"{task.ProjectName}|{task.Name}|{task.Deadline.ToString()}";

                        writer.WriteLine(line);

                    }

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show("Error while saving data: " + ex.Message);

            }

        }

    }

}


Once that is done, build the project, and then run it.


648c141bd3e8b.png


Choose a project name, a task, pick a deadline, and then click the “Add Task” button to display it in the ListBox.


Here’s what I have got after adding three tasks.


648c142a01296.png


All the information is stored in the data.txt file, which can be found in the project folder.


648c14384f43b.png


I hope you will find this application useful. And don’t forget that I am here to help with any piece of software your business may need.

Loader GIF