0.4 First Day Assignment

Overview

Continue your completed 0.3 First Day application. Add a school shopping list, observe what happens when the application closes, and then add buttons to save and load the text.

Goal: Explain the difference between text in a running application and text saved in a file. Saving writes the current text to ShoppingList.txt. Loading reads that saved text back into the shopping-list TextBox.

Acceptance Criteria

  • The existing 0.3 controls still work.
  • The shopping-list TextBox starts empty each time the application runs.
  • Clicking Save Shopping List writes the current text to ShoppingList.txt.
  • Closing the application without saving leaves the saved file unchanged.
  • Clicking Load Shopping List restores the file contents to the shopping-list TextBox.
  • The completed solution is submitted through the Feedback System, and its URL is submitted to Canvas.
Instructions
1.Open Your Existing Solution
A Back up and open the old .sln
1 Close Visual Studio if the solution is open
2 Back up the completed 0.3 solution folder

Name the backup First Day 0.3 Backup LastName.

3 Open LastName.sln from your original working folder

Keep the working folder and solution names unchanged. Continue working in the original folder, keeping the backup as your completed 0.3 checkpoint.

4 Run the application and verify the 0.3 controls still work

Fix any existing errors before continuing.

5 Close the application before editing
2.Add a TextBox
A Add the shopping-list TextBox
1 Open FirstDayScenario/MainWindow.xaml
2 Change the existing Window Height to 760

Leave the other Window attributes in place.

3 Add the shopping-list StackPanel

Keep the existing 0.3 controls. Inside the existing layout container, paste this StackPanel after the 0.3 StackPanel and before the existing textOutput TextBox.

MainWindow.xaml — add this StackPanel
<StackPanel HorizontalAlignment="Center" Margin="0,215,0,0" VerticalAlignment="Top" Width="300">
    <TextBlock Text="School Shopping List" FontWeight="Bold" Margin="5" />
    <TextBox x:Name="shoppingListTextBox" Height="160" Margin="5" AcceptsReturn="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" />
</StackPanel>
4 Move the output area below the shopping list

Replace the existing textOutput line with this line. It leaves room for the shopping list and the two buttons you will add later.

MainWindow.xaml — replace the textOutput line
<TextBox x:Name="textOutput" HorizontalAlignment="Center" Height="170" Margin="0,510,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="300" BorderThickness="3" BorderBrush="#FFB3B3B4" />
B Check Your Work: Text disappears after closing
1 Run the application
2 Type Notebook and Pens into the shopping-list TextBox

Enter one item per line.

3 Close the application
4 Run it again and verify the shopping-list TextBox is empty

The text existed in the running application. You have not added a way to save it, so it disappears when the application closes.

5 Close the application before continuing
3.Add a Save Button
A Add the button and create its event handler
1 Add the Save Shopping List button

In MainWindow.xaml, paste this line immediately after shoppingListTextBox, inside the shopping-list StackPanel.

MainWindow.xaml — add the Save button
<Button x:Name="SaveShoppingListButton" Content="Save Shopping List" Margin="5" />
2 Double-click Save Shopping List in the XAML designer

Visual Studio creates a Click event handler and opens MainWindow.xaml.cs.

3 Paste the supplied statements into the Save event handler

Find the generated SaveShoppingListButton_Click method. Paste only these two statements between its opening and closing braces.

Paste inside SaveShoppingListButton_Click
File.WriteAllText("ShoppingList.txt", this.shoppingListTextBox.Text);
this.textOutput.Text = "Shopping list saved.";

The designer connects the button through its XAML Click attribute. Its value must exactly match the generated method name, including capitalization.

4 Add the System.IO using statement

Add this line with the other using statements at the top of MainWindow.xaml.cs if it is not already present.

MainWindow.xaml.cs — using statements
using System.IO;
B Check Your Work: Save and inspect the file
1 Run the application
2 Type Notebook and Pens, one per line
3 Click Save Shopping List and verify the confirmation

The output area should say Shopping list saved..

4 Close the application
5 Find ShoppingList.txt in your working solution folder

Use File Explorer to search for ShoppingList.txt. With the usual Visual Studio run settings, it is under the project's bin output folder, possibly inside configuration or framework subfolders.

6 Open the file and verify it contains Notebook and Pens

The file still contains the text after the application closes. This is persistent storage.

7 Close the file viewer without editing the file
C Check Your Work: The TextBox still starts empty
1 Run the application again
2 Notice that the shopping-list TextBox is empty

Saving writes to the file; it does not read the file back into the TextBox. You will add loading in Step 4. Leave the application running for the next check.

D Check Your Work: Typing alone does not change the file
1 Type Binder and Markers into the running application

Enter one item per line.

2 Close the application without clicking Save Shopping List
3 Reopen ShoppingList.txt and verify it still contains Notebook and Pens

Open the file from the same location. Changing the TextBox changes the running application's text. The saved file stays the same until you click Save.

4 Close the file viewer without editing the file
E Check Your Work: Saving replaces the file contents
1 Run the application again
2 Type Binder and Markers again, one per line
3 Click Save Shopping List
4 Close the application
5 Reopen the file and verify it now contains Binder and Markers

Notebook and Pens should no longer be present. Each call to File.WriteAllText replaces the file's contents with the current shopping-list text.

6 Close the file viewer without editing the file
4.Add a Load Button
A Add the button and create its event handler
1 Add the Load Shopping List button

In MainWindow.xaml, paste this line immediately after the Save button, inside the same StackPanel.

MainWindow.xaml — add the Load button
<Button x:Name="LoadShoppingListButton" Content="Load Shopping List" Margin="5" />
2 Double-click Load Shopping List in the XAML designer

Visual Studio creates the Click event handler and opens MainWindow.xaml.cs.

3 Paste the supplied code into the Load event handler

Paste this code between the opening and closing braces of the generated LoadShoppingListButton_Click method.

Paste inside LoadShoppingListButton_Click
if (File.Exists("ShoppingList.txt"))
{
    this.shoppingListTextBox.Text = File.ReadAllText("ShoppingList.txt");
    this.textOutput.Text = "Shopping list loaded.";
}
else
{
    this.textOutput.Text = "Save a shopping list before loading.";
}

The File.Exists check lets the button display a message if there is no saved file yet. Keep this code inside the Load button's event handler so loading happens when you click the button.

B Check Your Work: Load the saved text
1 Run the application

The shopping-list TextBox should begin empty.

2 Click Load Shopping List
3 Verify that Binder and Markers appear in the shopping-list TextBox

The output area should say Shopping list loaded..

4 Open ShoppingList.txt and compare its contents with the TextBox

The shopping-list TextBox and the saved file should contain the same text.

5 Close the file viewer and leave the application running
C Check Your Work: Clear the TextBox and load again
1 Select all text in the shopping-list TextBox and delete it
2 Click Load Shopping List again without clicking Save first
3 Verify that Binder and Markers reappear

Clearing the TextBox did not change the file. Loading reads the same saved text again. Clicking Save while the TextBox is empty would instead replace the file's contents with empty text.

4 Close the application
Submission
5.Final Check and Submit
A Complete the final technical check
1 Build the solution and correct any build errors
2 Verify that the existing 0.3 controls still work
3 Verify that both new buttons pass all checks above

The TextBox starts empty on each run. Save updates the file; Load restores the saved text.

B Zip the solution
1 Close Visual Studio
2 Create a ZIP of the entire LastName solution folder
3 Open the ZIP and confirm the solution and project are present

Verify that LastName.sln and the FirstDayScenario project folder are included.

C Submit to the Feedback System
1 Open the Feedback System and select First Day 0.4
2 Upload the zipped Visual Studio solution
3 Review the results and correct any required issues

Resubmit an updated ZIP when needed.

D Submit the Feedback System URL to Canvas
1 Copy the Feedback System submission URL
2 Open the 0.4 First Day Assignment in Canvas
3 Submit the Feedback System URL as your Canvas submission