"

4. Build SingleSuggestion Window

Add UI Components

Let’s modify the UI of the SingleSuggestion window.

  1. Go to SingleSuggestion.xaml file.
  2. Modify backBtn and add a button (getSuggestionBtn),  text blocks (suggestionTxt), radio buttons (breakfastRbtn). Check the provided code below to understand better. It is for the most outer grid.
    <Grid>
        <Button x:Name="backBtn" Content="Back to Main"  HorizontalAlignment="Left" Margin="26,26,0,0" VerticalAlignment="Top" Width="140" Height="45" Click="backBtn_Click"/>
        <RadioButton GroupName="breakfast" Content="Breakfast" x:Name="breakfastRbtn" IsChecked="True" HorizontalAlignment="Left" Margin="248,124,0,0" VerticalAlignment="Top" RenderTransformOrigin="1.075,0.533"/>
        <RadioButton GroupName="breakfast" Content="Lunch or Dinner" HorizontalAlignment="Left" Margin="438,124,0,0" VerticalAlignment="Top"/>
        <Button x:Name="getSuggestionBtn" Content="Get Suggestion" HorizontalAlignment="Left" Margin="248,165,0,0" VerticalAlignment="Top" Width="294" Height="47"/>
        <TextBlock x:Name="suggestionTxt" Text="" TextAlignment="Center" FontWeight="Bold" FontSize="40" HorizontalAlignment="Left" Margin="26,261,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Height="84" Width="745"/>
    </Grid>
    

Add Database Context

This part is the same as the last chapter.

  1. Go to SingleSuggestion.xaml.cs file.
  2. Under declaration for ParentWindow, declare a private MealSuggestionContainer field named _context.
    private MealSuggestionContainer _context;
    
  3. Inside of the constructor, create a new MealSuggestionContainer for _context.
    //create data context
    _context = new MealSuggestionContainer();
    

Add Controller

  1. Go to SingleSuggestion.xaml.cs file.
  2. Add variables needed for getSuggestionBtn_Click function under declaration for _context.
    int lastDish = -1;
    bool lastBreakfast = true;
    
  3. Add getSuggestionBtn_Click function. Check the code below.
    private void getSuggestionBtn_Click(object sender, RoutedEventArgs e)
    {
        Dish[] candidates;
    
        try
        {
            //breakfast
            if ((bool)breakfastRbtn.IsChecked)
            {
                //get dishes for breakfast
                var dishes = from d in _context.Dishes
                                where d.Breakfast == true
                                select d;
    
                //populate candidates with them
                candidates = dishes.ToArray();
    
                //if users changed dish type (breakfast or not)
                if (!lastBreakfast)
                {
                    lastBreakfast = true;
    
                    //reset index for last dish
                    lastDish = -1;
                }
            }
            //lunch or dinner
            else
            {
                //get dishes for lunch or dinner
                var dishes = from d in _context.Dishes
                                where d.Breakfast == false
                                select d;
    
                //populate candidates with them
                candidates = dishes.ToArray();
    
                //if users changed dish type (breakfast or not)
                if (lastBreakfast)
                {
                    lastBreakfast = false;
    
                    //reset index for last dish
                    lastDish = -1;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    
        //when no dish in database for selected dish type (breakfast or not)
        if (candidates.Length == 0)
        {
            MessageBox.Show("Please add dishes first.");
            return;
        }
    
        //get random int for dish index
        Random random = new Random();
        int index = random.Next(0, candidates.Length);
    
        //make sure users do not same dish right after
        if(candidates.Length > 1)
        {
            while(lastDish == index)
            {
                index = random.Next(0, candidates.Length);
            }
        }
    
        //set lastDish with current one
        lastDish = index;
    
        //display to users
        suggestionTxt.Text = "How about " + candidates[index].Name + "?";
    }
    
  4. Go to SingleSuggestion.xaml file.
  5. Add Click="getSuggestionBtn_Click" for getSuggestionBtn button.

Check Your Code

After following this chapter, your app should be able to:

  • Suggest a random dish, not getting the same one as the previous run if possible (more than 1 dish in the database) on the SingleSuggestion window.

License

OER Lab guide Centennial College Copyright © by Centennial College OER Lab Students. All Rights Reserved.