由于.netcf里TextBox不像VC++中一样可以指定number的style,所以我们借助API来实现首先,定义一个类,来Pinvoke Win32 API
public class Win32Pinvoke
{
public enum InputMode { Spell = 0, T9 = 1, Numbers = 2, Text = 3 }
// Constants required for interop
const int GW_CHILD = 5;
const uint EM_SETINPUTMODE = 0x00DE;
[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);
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);
}
}
其次, 在textbox的GOTFOCUS消息中实现:
private void textBox_GotFocus(object sender, System.EventArgs e)
{
Win32Pinvoke.SetInputMode(this.textBox,Win32Pinvoke.InputMode.Numbers);
}
注:: 本方法只能应用于smartphone中,对pocket pc不起作用. Pocket pc里的正在研究中,当我研究出来时,也会贴到这里.谢谢大家!~