You will continue working in the same First Day project you used in 0.2. This week, you will add one button, follow a simple true/false decision, use the debugger to watch the program choose a path, and then connect that decision to an It is raining checkbox.
You do not need previous programming experience. Follow each step in order and use the checkboxes to keep track of where you are.
MainWindow.xaml and MainWindow.xaml.csChoose Clothing buttonname ends in .slnImportant: Do not rename the project files or classes. For this assignment, you are only renaming the outer workspace folder from 0.2 to 0.3.
MainWindow.xaml describes what appears in the window.MainWindow.xaml.cs contains the C# code that runs when the user interacts with the window.FirstDayScenario project if it is collapsedMainWindow.xamlMainWindow.xaml if neededMainWindow.xaml.csNewStudentButton_Click, HaveBreakfastButton_Click, and FillBackpackButton_ClickA button handler is a method that runs when a button is clicked. You will add the button in XAML first, then double-click it in the designer so Visual Studio creates the handler for you.
MainWindow.xamlStackPanel that contains the New Student, Have Breakfast, and Fill Backpack buttonsfillBackpackButton<Button x:Name="ChooseClothingButton" Content="Choose Clothing" Width="300" Margin="5" />MainWindow.xaml, switch to the visual designer if it is not already visibleChoose Clothing button in the window previewChoose Clothing buttonMainWindow.xaml.cs and create a new click-handler method for that buttonChooseClothingButton_ClickThe new handler should look similar to this:
private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
}The button in MainWindow.xaml is now connected to the ChooseClothingButton_Click method in MainWindow.xaml.cs. You will add the decision logic inside this method in the next step.
Choose Clothing button appears in the windowMainWindow.xaml.csChooseClothingButton_Click handler you addedprivate void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
bool isRaining = true;
if (isRaining)
{
this.textOutput.Text = "Wear a rain jacket.";
}
}if (isRaining)Choose Clothing. Visual Studio should pause on the breakpointisRaining. Confirm that its value is trueWhat happened? Because isRaining was true, the statements inside the if block ran.
falseMainWindow.xaml.cs, change only bool isRaining = true; to bool isRaining = false;if (isRaining)Choose ClothingisRaining and confirm that it is now falseThis is an important result: an if block runs only when its condition is true. Right now there is no instruction telling the program what to do when the condition is false.
MainWindow.xaml.cs and find the ChooseClothingButton_Click handlerbool isRaining = false; for the first testelse
{
this.textOutput.Text = "Wear a hoodie.";
}Your complete handler should now look like this:
private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
bool isRaining = false;
if (isRaining)
{
this.textOutput.Text = "Wear a rain jacket.";
}
else
{
this.textOutput.Text = "Wear a hoodie.";
}
}if (isRaining)Choose ClothingisRaining is falsebool isRaining = false; back to bool isRaining = true;Choose Clothing, and stop at the same breakpointWhat to notice: The same if/else can follow either path. The value of the condition determines which path runs.
MainWindow.xamlMainWindow.xamlChooseClothingButton line you added earlier<CheckBox x:Name="rainCheckBox" Content="It is raining" Width="300" Margin="5" />The checkbox is named rainCheckBox. C# will use that exact name to check whether the box is selected.
MainWindow.xaml.csbool isRaining = true; inside ChooseClothingButton_Clickbool isRaining = this.rainCheckBox.IsChecked == true;Your finished handler should now be:
private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
bool isRaining = this.rainCheckBox.IsChecked == true;
if (isRaining)
{
this.textOutput.Text = "Wear a rain jacket.";
}
else
{
this.textOutput.Text = "Wear a hoodie.";
}
}if (isRaining)Choose ClothingisRaining. It should be falseChoose ClothingisRaining. It should be trueFinal flow: checkbox state → isRaining → if/else → clothing message
FirstDayScenario project folderYou're finished when your 0.3 workspace has been submitted successfully to FBS.