Code Repo    |     RSS
MD's Technical Sharing



Monday, March 9, 2009

Disable full-screen edit mode in smartphone's multiline textbox

The multi-line textbox created by .NET CF on a smartphone does not allow inline editing. As soon as user presses ENTER, the textbox enters full-screen edit mode, and editing must be done there.


To understand what is going on here, we need to understand into native code. The textbox control in .NET CF is a wrapper around the native edit control, however the specific wrapping which occurs changes depending upon the value of the multiline property.


As the "Expandable Edit Controls" article available on MSDN documents (available at http://msdn2.microsoft.com/en-us/library/ms911985.aspx), to get an expandable edit control in native Win32 code requires two controls to be constructed. A normal edit control, followed by a up down spinner control. It is the spinner control that is drawing the arrow to the right hand edge of the textbox control and introducing the behaviour which enables bringing up a fullscreen edit window. The .NET Compact Framework is hiding this additional control/complexity by automatically creating the required controls whenever the multiline property is changed.


There doesn't appear to be a property or method exposed via the Compact Framework which will enable the user to decide how multiline text box controls should be handled.


Knowing this and by using a tool such as Remote Win Spy++ to see what is going on at the OS level, the following hack attempts to remove the spinner control and disable the ENTER key to prevent the textbox entering into full-screen edit mode


using System.Runtime.InteropServices;


[DllImport("coredll.dll")]

private static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);


private const int GW_HWNDNEXT = 2;


[DllImport("coredll.dll")]

private static extern IntPtr GetParent(IntPtr hWnd);


[DllImport("coredll.dll")]

private static extern int DestroyWindow(IntPtr hWnd);


[DllImport("coredll.dll")]

private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter,

int X, int Y, int cx, int cy, int uFlags);


private void menuItem1_Click(object sender, EventArgs e)

{

// Find the spinner control associated with the textbox

IntPtr spin = GetWindow(textBox1.Handle, GW_HWNDNEXT);

// Destroy the spinner

DestroyWindow(spin);

// Move the textbox control to take up the entire

// client space

IntPtr container = GetParent(textBox1.Handle);

SetWindowPos(textBox1.Handle, container, 0, 0,

textBox1.Width - 1, textBox1.Height, 0);

}


private void TextBox1_KeyDown(object sender, KeyEventArgs e)

{

// disable the ENTER key

if (e.KeyCode == Keys.ENTER) e.Handled = true;

}


Reference:

http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/8272ec73-920f-4c04-ad38-1bca35598317/

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.