Browse DevX
Sign up for e-mail newsletters from DevX


Write Apps for the Smartphone without Dumbing Down Your UI

Learn how to change the text input modes and improvise the ContextMenu and TabControl controls using the Smartphone 2003 SDK for the .NET Compact Framework.

he long-awaited Smartphone 2003 platform finally supports the .NET Compact Framework. With this support, developers can now write managed Smartphone applications using C# or VB.NET. However, the current Smartphone 2003 SDK for the .NET Compact Framework has some rough edges: Not all functions that a developer needs are provided in the class library, nor are they available in the controls Toolbox. For example, Context Menus and Tab controls are not natively supported, which makes navigating the user interface of a Smartphone application less intuitive. (See the sidebar, "Compact Framework Controls," for a full list of supported controls.)

Also, the functionality for changing the input modes (alphanumeric, numeric, or T9 [predictive text input]) for a TextBox control is not available as a managed class; instead platform invoke is required. So, in this article, I will show you some of the ways to overcome these limitations of the Smartphone 2003 SDK.

Changing Text Input Modes
Unlike Pocket PCs, a Smartphone does not contain a built-in SIP (Soft Input Panel) for data input (since the Smartphone does not support touch-screen). Instead, Smartphone users have to rely on the numeric keypad for alphanumeric input. Applications designed for the Smartphone must consider this limitation.

What You Need
Microsoft Visual Studio .NET 2003 and Smartphone 2003 SDK

For example, a textbox for entering phone numbers should only allow users to enter numeric inputs when using the numeric keypad. To do so, you can set the input mode of a TextBox control to accept either alphanumeric or numeric input. However, in the Smartphone 2003 SDK you cannot directly set the input mode of a TextBox control. Rather, you have to resort to Platform Invoke (P/Invoke) to use the API provided by the operating system.
Figure 1. Populating the Windows Form: Create two main menu items and four submenu items.
In this section, I will create a Smartphone application with two TextBox controls, one that accepts text and one that accepts numbers. This will illustrate how to set the input mode. And as an added bonus, the example also shows how to create a textbox that accepts multiple lines of input, instead of just one.

First, populate the default Windows Form with the following controls:
a. TextBox
b. MainMenu

The MainMenu control should have two main items named Exit and Input Modes. The Input Modes menu item should have four sub-menu items named Spell, T9, Numbers, and Text (see Figure 1):

You need to set the Multiline property of the TextBox2 control to True in order to stretch the TextBox control vertically.

To call an API at the operating system level using P/Invoke, you need to import the following namespace:
Imports System.Runtime.InteropServices
Next, declare the functions (within the class definition of the Windows Form) that you want to use to set the input modes of text boxes:
' Interop declarations
<DllImport("coredll.dll")> _
Private Shared Function GetCapture() As IntPtr
End Function

<DllImport("coredll.dll")> _
Private Shared Function GetWindow(ByVal hWnd As IntPtr, _
                      ByVal uCmd As Integer) As IntPtr
End Function

<DllImport("coredll.dll")> _
Private Shared Function SendMessage(ByVal hWnd As _
                     IntPtr, ByVal msg As UInt16, _
                     ByVal wParam As UInt16, _
                     ByVal lParam As UInt16) _
                     As UInt16
End Function
Declare the following enumeration and subroutine (within the class definition of the Windows Form) to set the input mode for the TextBox control:
' Input mode enumeration
Public Enum InputMode
    Spell = 0
    T9 = 1
    Numbers = 2
    Text = 3
End Enum
The SetInputMode() subroutine will invoke the SendMessage() API to change the text input mode of the TextBox control:
Public Shared Sub SetInputMode(ByVal ctrl As Control, _
                               ByVal mode As InputMode)
        Dim GW_CHILD As Integer = 5
        Dim EM_SETINPUTMODE As UInt16 = UInt16.Parse(222) 
        ' Get the handle for the current control
        ctrl.Capture = True
        Dim h As IntPtr = GetCapture()
        ctrl.Capture = False
        ' Get the child window for the control
        Dim hEditbox As IntPtr = GetWindow(h, GW_CHILD)
        ' Set the input mode
        SendMessage(hEditbox, EM_SETINPUTMODE, _
                    UInt16.Parse(0), _
End Sub
Service the Click event of each MenuItem control to call the SetInputMode() function with the respective mode:
Private Sub mnuSpell_Click(ByVal sender As _
                           System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles mnuSpell.Click
        SetInputMode(TextBox1, InputMode.Spell)
    End Sub

Private Sub mnuT9_Click(ByVal sender As System.Object, _
                        ByVal e As System.EventArgs) _
                        Handles mnuT9.Click
        SetInputMode(TextBox1, InputMode.T9)
End Sub

Private Sub mnuNumbers_Click(ByVal sender As _
                           System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles mnuNumbers.Click
        SetInputMode(TextBox1, InputMode.Numbers)
End Sub

Private Sub mnuText_Click(ByVal sender As _
                           System.Object, _
                           ByVal e As System.EventArgs) _
                           Handles mnuText.Click
        SetInputMode(TextBox1, InputMode.Text)
End Sub
Press F5 to debug and run the application. Figure 2 shows entering text in Text mode and Numeric mode respectively:

Figure 3 shows the second TextBox control supporting multiple lines of text. When the action button on the phone is depressed, the TextBox control expands to a full window, which is a normal, default behavior of the Smartphone.

Figure 2. Switching Input Modes: Text is entered on the left, while the numeric mode, right, only accepts numbers.
Figure 3. Multiple Lines: Normally limited to just one line, the sample application accepts multiple lines of input.

Author's Note: Note that T9 input is not supported on the emulator.

Thanks for your registration, follow us on our social networks to keep up-to-date