Make a simple tip calculator (Tipster) for Windows Phone in C#


You can view and download the app for free in the Windows Phone Store.

I wrote this program mostly to see how hard it was to submit an application to the Windows Phone Store. Submission did require a fair amount of work trying to figure out how to meet the submission Store’s requirements, but it wasn’t completely impossible. In total it probably took me 2 or 3 hours and I had to re-submit once because I made a mistake the first time. This application, which I named Tipster, is just about the simplest application you can build, however, so the testing process might take longer for a more complicated application.

To build an app for Windows Phone, install Visual Studio Express 2012 for Windows Phone (or some other version) from the Visual Studio download page.

Start a new project and pick the type of Windows Phone project you want to start. For this app I used the most basic template: Windows Phone App. When you create the new project, Visual Studio will ask you what phone platform you want to target. Generally the different versions are backwards compatible so, for example, if you target Windows Phone 7.1 then your app should run on Windows Phone 8.0. You should still use the emulator to test for every version that you want to support.

You should also test the application in whichever orientations you want to support. For example, you might want the program to work if the user turns the phone sideways.

When you run the application in Visual Studio, the application starts in an emulator by default. The Windows Phone 8 emulators require that your system have Hyper-V installed, which is only available if you have Windows 8 Professional. (At least on my 64-bit system. Perhaps it’s different on 32-bit systems.)




If you can’t run the Windows 8 emulators, all is not lost. When you create the project, you can target the Windows 7.1 platform. Then you can select either Windows 8 or Windows 7.1 emulators in Visual Studio. In the Standard toolbar, open the emulator dropdown shown on the right and select the emulator you want to use.

TIP: It takes a while to start the emulator. To save some time, don’t close the emulator after you test your program. Instead stop the program by clicking Visual Studio’s Stop Debugging button. That stops the program but not the emulator. Later when you run the program again, it can reuse the emulator that’s still running so it starts much more quickly.

When you have tested the program enough in the emulators, you can select the emulator dropdown’s Device option to download the program to your phone. Then when you run the program, Visual Studio will download the program and run it on your phone. This only works if you have done the following:

  1. First you need to unlock your phone for development by registering it. For instructions, go to How to register your phone for development.
  2. Next you need to use a USB cable to physically connect your phone to the computer.
  3. Finally the phone must be unlocked (you have to turn it on and enter your password) when you run the program on it.

Windows Phone apps are WPF/XAML so you use the XAML editor to edit the app’s window. The Tipster application uses a collection of StackPanels, Grids, TextBlocks, and Buttons to build its interface. Download the example to see how it works.

The Tipster program’s code (shown below) is quite simple.

// The value so far without the $.
private string Value = "";
// Process a button click.
private void ButtonClicked(object sender, RoutedEventArgs e)
{
// Process the button.
Button button = e.OriginalSource as Button;
string caption = button.Content.ToString();
switch (caption)
{
case "Back":
if (Value.Length > 0)
Value = Value.Substring(0, Value.Length - 1);
break;
case "Clear":
Value = "";
break;
default:
if (Value.Length < 9) Value += caption;
txtSubtotal.Text = Value;
break;
}
// Display the value.
DisplayValue();
}
// Display the subtotal value as currency.
private void DisplayValue()
{
// Parse the current value.
decimal subtotal;
if (!decimal.TryParse(Value, out subtotal))
{
subtotal = 0;
Value = "";
}
subtotal /= 100m;
// Display the parsed currency amount.
txtSubtotal.Text = subtotal.ToString("C");
// Display the tip values.
txt10Percent.Text = (subtotal * 0.10m).ToString("C");
txt15Percent.Text = (subtotal * 0.15m).ToString("C");
txt20Percent.Text = (subtotal * 0.20m).ToString("C");
txt25Percent.Text = (subtotal * 0.25m).ToString("C");
}

The program stores the text you have entered in the Value variable. When you click one of the program’s buttons, the ButtonClicked event handler executes. If you clicked the Clear button, the program clears Value. If you clicked the Back button, the program removes the last character from Value.

If you clicked a digit button, the program adds the digit to the Value string.

After processing the button, the program calls the DisplayValue method to perform calculations on the current value. That method tries to parse Value. If Value is a valid number, the code divides it by 100 (to convert it into dollars and cents) and displays Value as a currency amount. It then calculates 10%, 15%, 20%, and 25% tip amounts and displays them.




The picture on the right shows Tipster running in the Windows 7.1 emulator. The tiny text on the right edge of the application shows some diagnostic information such as the program’s frame rate. To disable that (so you can get a clean screen shot), open the App.xaml.cs file and comment out the following line:

Application.Current.Host.Settings.EnableFrameRateCounter = true;

Notice the little vertical tool bar to the right of the emulator. (If the toolbar isn’t visible, hover the mouse over the emulator somewhere other than over the application.) Its tools are:

  • Close.
  • Minimize.
  • Rotate emulator 90 degrees counterclockwise.
  • Rotate emulator 90 degrees clockwise.
  • Fit to screen.
  • Zoom.
  • Open additional tools.




The picture on the right shows the additional tools form. Click the Capture button to capture an image of the emulator. Then click the Save button to save the screen shot into a file.

Note that the Windows Phone Store requires that you use this tool to take a screen shot. If you press Alt-PrntScrn, you’ll get a screen shot but not one that is acceptable for submitting the application to the Windows Store.

After you build the program, test it in the desired emulators, test it on your phone, and take screen shots showing it in action, you need to submit it. There are a lot of steps so I won’t list them all here. Go to the Windows Phone Store Dev Center for instructions.



   

This entry was posted in mathematics, phone. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.