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
Name the backup First Day 0.3 Backup LastName.
Keep the working folder and solution names unchanged. Continue working in the original folder, keeping the backup as your completed 0.3 checkpoint.
Fix any existing errors before continuing.
Leave the other Window attributes in place.
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.
<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>
Replace the existing textOutput line with this line. It leaves room for the shopping list and the two buttons you will add later.
<TextBox x:Name="textOutput" HorizontalAlignment="Center" Height="170" Margin="0,510,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="300" BorderThickness="3" BorderBrush="#FFB3B3B4" />
Enter one item per line.
The text existed in the running application. You have not added a way to save it, so it disappears when the application closes.
In MainWindow.xaml, paste this line immediately after shoppingListTextBox, inside the shopping-list StackPanel.
<Button x:Name="SaveShoppingListButton" Content="Save Shopping List" Margin="5" />
Visual Studio creates a Click event handler and opens MainWindow.xaml.cs.
Find the generated SaveShoppingListButton_Click method. Paste only these two statements between its opening and closing braces.
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.
Add this line with the other using statements at the top of MainWindow.xaml.cs if it is not already present.
using System.IO;The output area should say Shopping list saved..
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.
The file still contains the text after the application closes. This is persistent storage.
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.
Enter one item per line.
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.
Notebook and Pens should no longer be present. Each call to File.WriteAllText replaces the file's contents with the current shopping-list text.
In MainWindow.xaml, paste this line immediately after the Save button, inside the same StackPanel.
<Button x:Name="LoadShoppingListButton" Content="Load Shopping List" Margin="5" />
Visual Studio creates the Click event handler and opens MainWindow.xaml.cs.
Paste this code between the opening and closing braces of the generated LoadShoppingListButton_Click method.
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.
The shopping-list TextBox should begin empty.
The output area should say Shopping list loaded..
The shopping-list TextBox and the saved file should contain the same text.
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.
The TextBox starts empty on each run. Save updates the file; Load restores the saved text.
Verify that LastName.sln and the FirstDayScenario project folder are included.
Resubmit an updated ZIP when needed.