Skip to content

Toilet List App - Step-by-Step Build

Windows Forms (.NET Framework)


We will build a simple classroom toilet tracking app.

Learning goals

By the end of this task you will:

  • Read data from a TXT file
  • Use ListBoxes
  • Use event-driven programming
  • Write reusable methods
  • Move data between UI components

STEP 0 - Create the project

Open Visual Studio

Create a Windows Forms App (.NET Framework)

Name it: ToiletListForm

STEP 1 - Design the form

In the Designer, add:

Three ListBoxes

Rename them in the Properties window:

Control Name
Left listBoxClassList
Middle listBoxWaitingList
Right listBoxInToilet

(Optional) Add labels.

STEP 2 - Create the text file

Create a file called:

ClassList.txt

Put this inside your project.

Where should it go? In Solution Explorer, place ClassList.txt directly under the ToiletListForm project (same level as Form1.cs and the .csproj file).

Example content:

Ava
Ben
Charlie
Dylan
Emma

Then:

Right-click the file in Solution Explorer

Properties -> Copy to Output Directory -> Copy if newer

This ensures the program can find it.

STEP 3 - Starter Code (this is where students begin typing)

Open Form1.cs.

You will see something like this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
}

This is your starting point.

STEP 4 - Add the file path variable

Students type this inside the class but above the constructor:

public partial class Form1 : Form
{
    // This stores the file name we want to read
    private readonly string classListFile = "ClassList.txt";

    public Form1()

Why here?

Because:

It belongs to the whole form

Every method needs access to it

It should not change while the program runs

This is called a field (class-level variable).

STEP 5 - Connect events in the constructor

Now add these lines inside the constructor:

public Form1()
{
    InitializeComponent();

    // Runs when the form loads
    this.Load += Form1_Load;

    // Double-click movement rules
    listBoxClassList.DoubleClick += listBoxClassList_DoubleClick;
    listBoxWaitingList.DoubleClick += listBoxWaitingList_DoubleClick;
    listBoxInToilet.DoubleClick += listBoxInToilet_DoubleClick;
}

Why do this?

Instead of using the Designer:

It prevents mistakes

Makes the program easier to debug

Ensures the events always work

STEP 6 - Create the Form Load event

Add this below the constructor:

private void Form1_Load(object sender, EventArgs e)
{
    LoadClassList();
}

What does this do?

When the form opens, it runs the method that loads the class list.

STEP 7 - Read the TXT file and fill the class list

Add this method:

// Reads the text file and fills the class list
private void LoadClassList()
{
    listBoxClassList.Items.Clear();

    if (!File.Exists(classListFile))
    {
        MessageBox.Show("ClassList.txt not found.");
        return;
    }

    var students = File.ReadAllLines(classListFile)
                       .Select(line => line.Trim())
                       .Where(line => !string.IsNullOrWhiteSpace(line))
                       .Distinct()
                       .ToArray();

    listBoxClassList.Items.AddRange(students);
}

What this does

Checks the file exists

Reads each line

Removes blank lines

Removes duplicates

Adds students to the ListBox

STEP 8 - Add movement rules

Now we write the double-click events.

Class -> Waiting

private void listBoxClassList_DoubleClick(object sender, EventArgs e)
{
    MoveStudent(listBoxClassList, listBoxWaitingList);
}

Waiting -> In Toilet

private void listBoxWaitingList_DoubleClick(object sender, EventArgs e)
{
    MoveStudent(listBoxWaitingList, listBoxInToilet);
}

In Toilet -> Class

private void listBoxInToilet_DoubleClick(object sender, EventArgs e)
{
    MoveStudent(listBoxInToilet, listBoxClassList);
}

Why do this?

Instead of repeating code, we call one reusable method.

This is good programming practice.

STEP 9 - Create the reusable method

Add this at the bottom of the class:

private void MoveStudent(ListBox fromList, ListBox toList)
{
    if (fromList.SelectedItem == null) return;

    string student = fromList.SelectedItem.ToString();

    fromList.Items.Remove(student);

    if (!toList.Items.Contains(student))
    {
        toList.Items.Add(student);
    }
}

What this does

Gets the selected student

Removes them from the current list

Adds them to the new list

Prevents duplicates


COMPLETE!

You now have:

  • File reading
  • Event-driven UI
  • ListBox control
  • Reusable methods
  • Data movement between components

This is excellent preparation for Unit 3 Outcome 1 programming skills.