How To Design A Calculator In UWP

To develop apps for UWP you must have Windows 10 and Visual Studio 2015 installed. You can install visual studio community for development purposes which is absolutely free.

Start Visual Studio and create a project either by clicking New Project from the start screen or going to File, New, then Project.

We will design our calculator using grid panel. A grid can contain multiple rows and columns and we can define height and width for each row and column respectively although by default it has just one row and one column.

Our calculator design will have 4 columns as 4 is the maximum number of buttons in any row and 7 rows. To do so, we will need to add the following lines of code to our MainPage.xaml file.

  1. <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">  
  2.     <Grid.ColumnDefinitions>  
  3.         <ColumnDefinition />  
  4.         <ColumnDefinition />  
  5.         <ColumnDefinition />  
  6.         <ColumnDefinition />   
  7.     </Grid.ColumnDefinitions>  
  8.     <Grid.RowDefinitions>  
  9.         <RowDefinition />  
  10.         <RowDefinition/>  
  11.         <RowDefinition/>  
  12.         <RowDefinition/>  
  13.         <RowDefinition/>  
  14.         <RowDefinition/>  
  15.         <RowDefinition/>   
  16.   </Grid.RowDefinitions>  
  17. </Grid>  
The <Grid.ColumnDefinition> tag is used to add columns, now the column can be of the three different sizes.

 

  • Fixed

  • Auto (takes as much space as needed by the contained content).

  • * (takes as much space as available) and if there is a number prefix before * e.g. 10* , that means 10% of the remaining grid. If we had two columns of width 1* and 2*, the second column will take twice as much space as the first one.

As we want equally sized columns we will simply omit the width specification because by default it is 1* which will divide the available grid space among all the columns equally. Same logic applies to rows but in context of height.

Like arrays both rows and columns also start form 0 and we want our first row to have sort of a heading indicating that it is a calculator app.

  1. <Border Grid.Row="0" Background="White" Grid.ColumnSpan="4">  
  2.     <TextBox HorizontalAlignment="Center" IsReadOnly="True" Text="Calculator" Foreground="Black" BorderBrush="Transparent" TextAlignment="Center" FontSize="70"></TextBox>  
  3. </Border>  
Using Grid.Row property we will set the row in which we want our UI control to be displayed and Grid.ColumnSpan will allow us to set the number of columns that this control can span. Inside the border tag we have a textbox which will basically serve as a label but since label tag is not supported in a UWP project we will use textbox with its IsReadOnly property set to True as this will make it non-editable.

To set the background color of rows the following code will be used.
  1. <Grid Grid.Row="1" Background="WhiteSmoke" Grid.ColumnSpan="4"></Grid>  
  2. <Grid Grid.Row="2" Background="WhiteSmoke" Grid.ColumnSpan="4"></Grid>  
  3. <Grid Grid.Row="3" Background="WhiteSmoke" Grid.ColumnSpan="4"></Grid>  
  4. <Grid Grid.Row="4" Background="WhiteSmoke" Grid.ColumnSpan="4"></Grid>  
  5. <Grid Grid.Row="5" Background="WhiteSmoke" Grid.ColumnSpan="4"></Grid>  
  6. <Grid Grid.Row="6" Background="WhiteSmoke" Grid.ColumnSpan="4"></Grid>  
We will add two textboxes, one where user's entered value will be displayed and the second one for displaying result. The following code can be used for this purpose.
  1. <TextBox Grid.Row="1" Grid.ColumnSpan="4" TabIndex="1" VerticalAlignment="Top" HorizontalAlignment="Center" PlaceholderText="Enter Value" Width="300" BorderBrush="Transparent" TextAlignment="Right" FontSize="20" Margin="0,10,0,5" Height="40"></TextBox>  
  2. <TextBox HorizontalAlignment="Center" Grid.ColumnSpan="4" Grid.Row="1" TextAlignment="Left" PlaceholderText="Result" Width="300" BorderBrush="Transparent" Margin="0,50,0,5" FontSize="20" Height="40"></TextBox>  
Most of the properties in TextBox tag are self-explainable, the TabIndex property is used to set the Tab Order and as we want the focus to be on the "enter value" TextBox when our application first loads and the simplest way to achieve this is through the use of TabIndex property.

Margin (left, top, right, bottom) property allows you to set the outer margin of a UI control.

Now we will add buttons for '+', '-', '*' and '/' using the following code.
  1. <Button VerticalAlignment="Top" Width="80" Content="+" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="0" Grid.Row="2" FontSize="40"></Button>  
  2. <Button VerticalAlignment="Top" Width="80" Content="-" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="1" Grid.Row="2" FontSize="40"></Button>  
  3. <Button VerticalAlignment="Top" Width="80" Content="*" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="2" Grid.Row="2" FontSize="40"></Button>  
  4. <Button VerticalAlignment="Top" Width="80" Content="/" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="3" Grid.Row="2" FontSize="40"></Button>  
The same will be done for the next row which contains 7, 8, 9 but as it also contains the '=' button and for design purposes it is a little longer than the rest, so we will set the value of it's Grid.RowSpan property to 4 and BorderBrush to Black, which will make it pop out a bit than the rest.
  1. <Button VerticalAlignment="Top" Width="80" Content="7" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="0" Grid.Row="3" FontSize="40"></Button>  
  2. <Button VerticalAlignment="Top" Width="80" Content="8" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="1" Grid.Row="3" FontSize="40"></Button>  
  3. <Button VerticalAlignment="Top" Width="80" Content="9" Background="Transparent" Foreground="Black" Margin="0,20,0,0" Grid.Column="2" Grid.Row="3" FontSize="40"></Button>  
  4. <Button VerticalAlignment="Center" BorderBrush="Black" Height="340" Width="70" Content="=" Background="Transparent" Foreground="Black" Grid.Column="4" Grid.Row="3" Grid.RowSpan="4" Margin="10,0,10,0" FontSize="48" />  
The next three rows will be the exact same, and the code for those rows is as below.
  1. <!--5th Row-->   
  2. < Button VerticalAlignment = "Top"  
  3.     Width = "80"  
  4.     Content = "4"  
  5.     Background = "Transparent"  
  6.     Foreground = "Black"  
  7.     Margin = "0,20,0,0"  
  8.     Grid.Column = "0"  
  9.     Grid.Row = "4"  
  10.     FontSize = "40" >   
  11. < /Button>   
  12. < Button VerticalAlignment = "Top"  
  13.     Width = "80"  
  14.     Content = "5"  
  15.     Background = "Transparent"  
  16.     Foreground = "Black"  
  17.     Margin = "0,20,0,0"  
  18.     Grid.Column = "1"  
  19.     Grid.Row = "4"  
  20.     FontSize = "40" >   
  21. < /Button>  
  22. < Button VerticalAlignment = "Top"  
  23.     Width = "80"  
  24.     Content = "6"  
  25.     Background = "Transparent"  
  26.     Foreground = "Black"  
  27.     Margin = "0,20,0,0"  
  28.     Grid.Column = "2"  
  29.     Grid.Row = "4"  
  30.     FontSize = "40" >   
  31. < /Button>  
  32. <!--6th Row-->  
  33. < Button VerticalAlignment = "Top"  
  34.     Width = "80"  
  35.     Content = "1"  
  36.     Background = "Transparent"  
  37.     Foreground = "Black"  
  38.     Margin = "0,20,0,0"  
  39.     Grid.Column = "0"  
  40.     Grid.Row = "5"  
  41.     FontSize = "40" >   
  42. < /Button>   
  43. < Button VerticalAlignment = "Top"  
  44.     Width = "80"  
  45.     Content = "2"  
  46.     Background = "Transparent"  
  47.     Foreground = "Black"  
  48.     Margin = "0,20,0,0"  
  49.     Grid.Column = "1"  
  50.     Grid.Row = "5"  
  51.     FontSize = "40" >   
  52. < /Button>   
  53. < Button VerticalAlignment = "Top"  
  54.     Width = "80"  
  55.     Content = "3"  
  56.     Background = "Transparent"  
  57.     Foreground = "Black"  
  58.     Margin = "0,20,0,0"  
  59.     Grid.Column = "2"  
  60.     Grid.Row = "5"  
  61.     FontSize = "40" >   
  62. < /Button>  
  63. <!--7th Row-->  
  64. < Button VerticalAlignment = "Top"  
  65.     Width = "80"  
  66.     Content = "0"  
  67.     Background = "Transparent"  
  68.     Foreground = "Black"  
  69.     Margin = "0,20,0,0"  
  70.     Grid.Row = "6"  
  71.     Grid.Column = "0"  
  72.     FontSize = "40" >   
  73. < /Button>   
  74. < Button VerticalAlignment = "Top"  
  75.     Width = "80"  
  76.     Content = "."  
  77.     Background = "Transparent"  
  78.     Foreground = "Black"  
  79.     Margin = "0,20,0,0"  
  80.     Grid.Row = "6"  
  81.     Grid.Column = "1"  
  82.     FontSize = "40" >   
  83. < /Button>   
  84. < Button VerticalAlignment = "Top"  
  85.     Width = "80"  
  86.     Content = "Clear"  
  87.     Background = "Transparent"  
  88.     Foreground = "Black"  
  89.     Margin = "0,20,0,0"  
  90.     Grid.Row = "6"  
  91.     Grid.Column = "2"  
  92.     FontSize = "25" >   
  93. < /Button>  
Make sure you type/paste all that code inside of the parent Grid tag. When you are  are done, run the app by pressing F5 or clicking on the green triangle button.

Viola! This is how your output should look like.

Calculater

If you will try to resize the window you'll find out that our app is not responsive i.e. the layout does not adjust itself according to the width of window. But don't worry about that we will learn how to do that in a future post.

 


Similar Articles