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.
Important: 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.A 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.
<Button x:Name="ChooseClothingButton" Content="Choose Clothing" Width="300" Margin="5" />The 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.
private void ChooseClothingButton_Click(object sender, RoutedEventArgs e)
{
bool isRaining = true;
if (isRaining)
{
this.textOutput.Text = "Wear a rain jacket.";
}
}What happened? Because isRaining was true, the statements inside the if block ran.
This 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.
else
{
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.";
}
}What to notice: The same if/else can follow either path. The value of the condition determines which path runs.
<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.
bool 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.";
}
}Final flow: checkbox state → isRaining → if/else → clothing message
You're finished when your 0.3 workspace has been submitted successfully to FBS.