Create a Standalone Pocket PC App
The Pocket PC needs a better calculator. Fortunately, the .NET Compact Framework makes it easy to build one.
by Bill Wagner
May 2003 Issue
I'd had enough. I tried for a few months to use the calculator application that comes with the Pocket PC. It's one of the lamest bits of software I've ever used. I'd been happy with the old HP 15C Reverse Polish Notation (RPN)–style calculator I'd used for a long time, and I wanted that same functionality on my new device. I set out to solve this deficiency by writing my own version. This gave me a great opportunity to create a small sample that demonstrates how quickly you can build a standalone application for your Pocket PC using the .NET Compact Framework. So, on one of those cold afternoons we get in the upper Midwest, I wrote a better calculator. I use it now instead of the built-in version for all my work.
First, you need to build the user interface for the calculator. Start VS.NET 2003 or one of the final betas and create a new Smart Device application. Start with a blank form. You must create the calculator interface for your application (see Figure 1). Add buttons for each of the digits, common mathematical operations, and a few more-sophisticated functions. I chose a subset of the functions on my old HP. I'll undoubtedly add more functions over time as I use the application. You can download the code and add your own functions as well.
Creating the form is almost exactly the same as it is for any .NET application, but you need to be aware of a few subtle differences. The first is that you must configure the edit control as a multiline edit control, even though you use it as a single-line edit control. The reason is that the Compact Framework version of the single-line edit control doesn't support right or center alignment. The number control must be multiline in order to display right-aligned text.
I wanted to use some of the new gradient backgrounds to give the calculator a little pizzazz. No such luck, though; they aren't part of the Compact Framework (see Figure 2). You must add event handlers for all the buttons in the calculator before you move on to storage for the values. Storing all the values for the RPN calculator is a simple matter of using a stack (see the sidebar, "A Primer on RPN Calculators"). Any value the user enters gets pushed onto the stack. When the user enters a function or operator, the program pops the necessary values off the stack. Add a stack as a member of the Form class:
private System.Collections.Stack
RPNStack;
This function adds a value to the stack:
private void pushCurrentValue ()
{
if (!double.IsNaN (theValue))
{
// Take the value:
// Put it on the stack.
RPNStack.Push (theValue);
DisplayFormatValue (theValue);
}
else
{
theValue = 0;
displayBox.Text = "Error";
}
// Start a new Entry:
currentEntry.Remove
(0,currentEntry.Length);
}
The preceding function checks to see if the current entry is a number or the result of an error (such as divide by 0). Then, it pushes the value onto the stack and displays the formatted version of the number.
Back to top
|