Guest pagem2uk Posted May 1, 2004 Report Posted May 1, 2004 I can change the input mode through DllImport of GetFocus. Is there a way to achieve this with managed code?
Guest Peter Foot Posted May 6, 2004 Report Posted May 6, 2004 No, you have to P/Invoke to the API functions to send the EM_SETINPUTMODE message to the control. There is no method to set this directly with the control properties. Peter
Guest hanaka Posted May 13, 2004 Report Posted May 13, 2004 Try this The input mode can be set using GetFocus and SendMessage APIs according to the code below: '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 <DllImport("coredll.dll")> _ Public Shared Function GetFocus() As IntPtr End Function <DllImport("coredll.dll")> _ 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; }
Guest Brody Posted August 23, 2004 Report Posted August 23, 2004 This seems to change the default input when a control receives focus but if the control has focus when the form is loaded it does not obtain the default input, it is only when it loses and then re-gains focus that this is solved.
Guest Mike Wagstaff Posted September 3, 2004 Report Posted September 3, 2004 Hanaka: Thanks for the code - works great! This seems to change the default input when a control receives focus but if the control has focus when the form is loaded it does not obtain the default input, it is only when it loses and then re-gains focus that this is solved. Maybe you could use a GotFocus event handler for your input boxes that sets the input mode appropriately? That's what I do, and it works fine every time for me.
Guest Peter Foot Posted September 6, 2004 Report Posted September 6, 2004 This seems to change the default input when a control receives focus but if the control has focus when the form is loaded it does not obtain the default input, it is only when it loses and then re-gains focus that this is solved. You could add a similar block of code to your form_load method so the defaults are set when the form initially loads. Also using Capture GetCapture is usually more reliable (less side effects) than using Focus/GetFocus to get a control's handle) TextBox1.Capture = true hWnd = GetCapture() TextBox1.Capture = false; SendMessage(hWnd, EM_SETINPUTMODE, 0, EIM_NUMBERS); The P/Invoke declaration for GetCapture is very similar:- [DllImport("coredll.dll")] public static extern IntPtr GetCapture(); Also the control should persist this setting so you only need to set it the once, not each time the control receives focus. Peter
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now