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
TextBox starts empty each time the application runs.ShoppingList.txt.TextBox.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.
TextBoxFirstDayScenario/MainWindow.xamlLeave the other Window attributes in place.
StackPanelKeep 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" />
Text disappears after closingTextBoxEnter one item per line.
TextBox is emptyThe text existed in the running application. You have not added a way to save it, so it disappears when the application closes.
Save Shopping List buttonIn MainWindow.xaml, paste this line immediately after
shoppingListTextBox
, inside the shopping-list StackPanel.
<Button x:Name="SaveShoppingListButton" Content="Save Shopping List" Margin="5" />
Save Shopping List in the XAML designerVisual 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.
System.IO using statementAdd this line with the other using statements at the top of MainWindow.xaml.cs if it is not already present.
using System.IO;Click Save Shopping List and verify the confirmationThe output area should say Shopping list saved..
ShoppingList.txt in your working solution folderUse 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.
TextBox still starts emptyTextBox is emptySaving 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.
ShoppingList.txt and verify it still contains Notebook and PensOpen 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.
Click Save Shopping ListNotebook and Pens should no longer be present. Each call to File.WriteAllText replaces the file's contents with the current shopping-list text.
Load Shopping List buttonIn MainWindow.xaml, paste this line immediately after the Save button, inside the same StackPanel.
<Button x:Name="LoadShoppingListButton" Content="Load Shopping List" Margin="5" />
Load Shopping List in the XAML designerVisual 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.
Click Load Shopping ListTextBoxThe output area should say Shopping list loaded..
ShoppingList.txt and compare its contents with the TextBoxThe shopping-list TextBox and the saved file should contain the same text.
TextBox and load againTextBox and delete itClick Load Shopping List again without clicking Save firstClearing 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.