Universal Windows Platform – Lucky Lotto

Lucky Lotto demonstrates how to use StackPanel to display a set of selected Lottery Numbers

Step 1

If not already, follow Setup and Start on how to Install and get Started with Visual Studio 2017 or in Windows 10 choose Start, and then from the Start Menu find and select Visual Studio 2017.

vs2017

Step 2

Once Visual Studio Community 2017 has started, from the Menu choose File, then New then Project…

vs2017-file-new-project

Step 3

From New Project choose Visual C# from Installed, Templates then choose Blank App (Universal Windows) and then type in a Name and select a Location and then select Ok to create the Project
vs2017-new-project-window

Step 4

Then in New Universal Windows Project you need to select the Target Version this should be at least the Windows 10 Fall Creators Update (10.0; Build 16299) and the Minimum Version to be the same.

vs2017-target-platform

The Target Version will control what features your application can use in Windows 10 so by picking the most recent version you’ll be able to take advantage of those features. To make sure you always have the most recent version, in Visual Studio 2017 select Tools Extensions and Updates… then and then see if there are any Updates

Step 5

Once done select from the Menu, Project, then Add New Item…

vs2017-project-add-new-item

Step 6

From the Add New Item window select Visual C#, then Code from Installed then select Code File from the list, then type in the Name as Library.cs before selecting Add to add the file to the Project

vs2017-add-new-item-library

Step 7

Once in the Code View for Library.cs the following should be entered:

using System;
using System.Collections.Generic;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;

public class Library
{
    private Random _random = new Random((int)DateTime.Now.Ticks);

    private List<int> Choose()
    {
        int number;
        List<int> numbers = new List<int>();
        while ((numbers.Count < 6)) // Select 6 Numbers
        {
            number = _random.Next(1, 60);
            if ((!numbers.Contains(number)) || (numbers.Count < 1))
            {
                numbers.Add(number); // Add if not Chosen or None
            }
        }
        numbers.Sort();
        return numbers;
    }

    public void New(StackPanel stack)
    {
        stack.Children.Clear();
        List<int> numbers = Choose();
        foreach (int number in numbers)
        {
            Grid container = new Grid()
            {
                Height = 60,
                Width = 60,
                Background = new SolidColorBrush(Colors.WhiteSmoke)
            };
            Ellipse ball = new Ellipse()
            {
                Height = 50,
                Width = 50,
                StrokeThickness = 5,
                Margin = new Thickness(2),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            if (number >= 1 && number <= 9)
            {
                ball.Fill = new SolidColorBrush(Colors.White);
            }
            else if (number >= 10 && number <= 19)
            {
                ball.Fill = new SolidColorBrush(Colors.Cyan);
            }
            else if (number >= 20 && number <= 29)
            {
                ball.Fill = new SolidColorBrush(Colors.Magenta);
            }
            else if (number >= 30 && number <= 39)
            {
                ball.Fill = new SolidColorBrush(Colors.LawnGreen);
            }
            else if (number >= 40 && number <= 49)
            {
                ball.Fill = new SolidColorBrush(Colors.Yellow);
            }
            else if (number >= 50 && number <= 59)
            {
                ball.Fill = new SolidColorBrush(Colors.Purple);
            }
            container.Children.Add(ball);
            TextBlock label = new TextBlock()
            {
                Foreground = new SolidColorBrush(Colors.Black),
                FontSize = 16,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center,
                Text = number.ToString()
            };
            container.Children.Add(label);
            stack.Children.Add(container);
        }
    }
}

In the Library.cs there are using statements to include the necessary functionality. Random is used to create the numbers needed, Choose is used to select the set of Numbers to be used. New sets up a StackPanel passed in and Adds some Grid Controls with a TextBlock to show the value of a Number and an Ellipse to represent each Lottery Ball with the approppriate background matched to the UK Lottery Balls

Step 8

In the Solution Explorer select MainPage.xaml

vs2017-mainpage

Step 9

From the Menu choose View and then Designer

vs2017-view-designer

Step 10

The Design View will be displayed along with the XAML View and in this between the Grid and /Grid elements, enter the following XAML:

<Viewbox>
	<StackPanel Margin="50" Name="Display" HorizontalAlignment="Center" Orientation="Horizontal"/>
</Viewbox>
<CommandBar VerticalAlignment="Bottom">
	<AppBarButton Name="New" Icon="Page2" Label="New" Click="New_Click"/>
</CommandBar>

The first block of XAML the main user interface of the Application, this features a StackPanel where the selected Numbers will appear. The second block of XAML is is the CommandBar which contains the New – to pick a new set of Numbers

Step 11

From the Menu choose View and then Code

vs2017-view-code

Step 12

Once in the Code View, below the end of public MainPage() { … } the following Code should be entered:

Library library = new Library();

private void New_Click(object sender, RoutedEventArgs e)
{
	library.New(Display);
}

Below the MainPage() Method an instance of the Library Class is created, then in the New_Click Event the New Method picks a new set of Numbers to appear on the StackPanel

Step 13

That completes the Universal Windows Platform Application so Save the Project then in Visual Studio select the Local Machine to run the Application

vs2017-local-machine

Step 14

After the Application has started running you can then tap on the New button to select some Numbers

ran-lucky-lotto

Step 15

To Exit the Application select the Close button in the top right of the Application

vs2017-close

Creative Commons License

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s