可以根据以下代码,使用 GetFocus 和 SendMessage API 来设置输入模式:
'VB
Imports System.Runtime.InteropServices
Public Const EM_SETINPUTMODE As Integer = &HDE
Public Const EIM_SPELL As Integer = 0
Public Const EIM_AMBIG As Integer = 1
Public Const EIM_NUMBERS As Integer = 2
_
Public Shared Function GetFocus() As IntPtr
End Function
_
Public Shared Function SendMessage(ByVal hWnd As IntPtr, _
ByVal Message As Integer, ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
End Function
'Sample use setting TextBox to number input
Private Sub txtAmount_GotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles txtAmount.GotFocus
Dim hWnd As IntPtr
hWnd = Me.GetFocus()
SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS)
txtAmount.SelectionStart = txtAmount.Text.Length
End Sub
//C#
using System.Runtime.InteropServices;
public const uint EM_SETINPUTMODE = 0xDE;
public const uint EIM_SPELL = 0;
public const uint EIM_AMBIG = 1;
public const uint EIM_NUMBERS = 2;
[DllImport("coredll.dll")]
public static extern IntPtr GetFocus();
[DllImport("coredll.dll")]
public static extern int SendMessage(IntPtr hWnd,
uint Message, uint wParam, uint lParam);
// Sample use setting TextBox to number input
private void Form1_Load(object sender, System.EventArgs e)
{
txtAmount.GotFocus +=
new System.EventHandler(txtAmount_GotFocus);
}
private void txtAmount_GotFocus(object sender, System.EventArgs e)
{
IntPtr hWnd;
hWnd = GetFocus();
SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS);
txtAmount.SelectionStart = txtAmount.Text.Length;
}