Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
A couple of days ago I sent out a link to a Smartphone “Tip” calculator, quite handy if you don’t want to be seen counting on your fingers or using napkins or slide rules to figure out how much tip to pay. This seems like a really useful, simple application, but… the applicaiton seems to have some input wierdness when entering the amount to pay (if you download the application you should quickly see what I mean).
So, having a few minutes behind stage at TechEd Europe before my keynote demo started I decided to write my own version, should be quick and easy, right ? – I have my Typhoon Smartphone, a USB sync cable (great for charging the device on the road!), Visual Studio .NET 2003, ok, good, we’re all set.
Throwing together the UI of the applicaiton was trivial, a TextBox, a couple of ComboBox’s, a panel (so I can have the breakdown of who pays what in a different color) and some Labels, the UI was ‘up and running’ in just a couple of your earth minutes, ok, so now what…
Here’s the first issue, the TextBox used to enter the price is, by default letting my enter T9 characters, I don’t want to have to switch to numeric input every time I want to enter a value, so I need a way to change the TextBox to numeric input at start time – there isn’t a straight forward way to do this (no exposed methods on the control), so it’s time to roll up the sleeves and start a) Coding, or b) doing a search of Google/MSN Search.
Here’s what I came up with…
// Interop declarations
[DllImport("coredll.dll", EntryPoint="GetCapture")]
private static extern IntPtr GetCapture();
[DllImport("coredll.dll", EntryPoint="GetWindow")]
private static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("coredll.dll", EntryPoint="SendMessage")]
private static extern uint SendMessage(IntPtr hWnd, uint msg, uint wParam,
uint lParam);
// Constants required for interop
const int GW_CHILD = 5;
const uint EM_SETINPUTMODE = 0x00DE;
public static void SetInputMode(Control ctrl, InputMode mode)
{
// Get the handle for the current control
ctrl.Capture = true;
IntPtr h = GetCapture();
ctrl.Capture = false;
// Get the child window for the control
IntPtr hEditbox = GetWindow(h, GW_CHILD);
// Set the input mode
SendMessage(hEditbox, EM_SETINPUTMODE, 0, (uint)mode);
}
// Input mode enumeration
public enum InputMode
{
Spell = 0,
T9 = 1,
Numbers = 2,
Text = 3
}
So, now, we can just do the following…
SetInputMode(MyTextBox, InputMode.Numbers);
That’s it, right ? – no, not quite, now we need to have some way of entering the period ‘.’, the ‘*’ key switches from T9 to Abc, to abc etc… so I decided to hijack the ‘#’ key – a quick override of TextChanged on the TextBox, I check to see if the last character is ‘ ‘ (the default behavior of the ‘#’ key in numeric input mode) and replace the character with a period, but, all is not well in numeric input land… when we replace the existing string the text input caret moves to the start of the string (this is the same odd behavior we see in the Tip Calculator I linked to a couple of days ago) – so I need to move the caret to the end of the string, there isn’t an obvious SetCaretPosition or similar function to call on the TextBox, you can set the caret position by calling TextBox.SelectionStart
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
string strText=MealCost.Text;
if (MealCost.Text.Length > 0)
{
if (strText[strText.Length-1] == ' ')
{
strText=strText.Substring(0,strText.Length-1);
strText+=".";
MealCost.Text=strText;
MealCost.SelectionStart=strText.Length;
}
}
}
W’re done, right ? – not quite, one final small hurdle to jump over – when calculating how much each member of a party of three would pay for a meal costing $100.00 you get a value of 33.3333333333 displayed in the application, we need to round the value to two decimal places – the .NET Compact Framework makes coding this type of application a snap – take a look at Math.Round( ); – takes a double, returns a double.
To set the Labels the code looks something like…
EachPay_Value.Text=Math.Round(dblCostPerPerson,2).ToString();
Snap! – that’s all there is to it… the numeric text input works well, yes, I know I’m not trapping multiple decimal points in the application, but this was written in less than five minutes behind stage at the TechEd Europe keynote!
Here’s my version of the Tip Calculator – drop this onto your Smartphone \Windows\Start Menu – and you’re all set – enjoy…
– Mike
Comments
- Anonymous
July 05, 2005
Mike - if you had watched Paramesh's session on Tuesday, you would have seen the very same thing being done with very little code thanks to the new support for inheriting controls in VSD and .NETCF 2.0 :-) - Anonymous
July 06, 2005
yep, but I only had VS 2003 installed on my laptop, so needed to use what was available.
Had I been in the office I have my desktop PC installed with VS 2005...
- Mike - Anonymous
July 06, 2005
Okay, so three split a $100 tab, each pays $33.33 (you round). um... the manager may have issue with that math. - Anonymous
May 26, 2009
PingBack from http://castironbakeware.info/story.php?title=windows-embedded-blog-smartphone-tip-calculator-numeric-input-my