在您的PPC應(yīng)用程序中使用InputPanel控件

2010-08-28 10:50:01來源:西部e網(wǎng)作者:

Christian Forsberg
businessanyplace.net

September 2003

Applies to:
   Microsoft® .NET Compact Framework version 1.0
   Microsoft Visual Studio® .NET 2003
   Windows Mobile-based Pocket PCs
   Windows Mobile 2003 Second Edition software for Pocket PCs

Summary: Learn how the InputPanel control can be used to create an efficient user interface in your Pocket PC applications. This article will explain why it is important and show you how it can be done. (5 printed pages)

Download inputpanel_control.exe

Contents

Why SIP Support?
The InputPanel Control
InputPanel Sample
Code Walkthrough
Conclusion

Why SIP Support?

Most Windows Mobile-based Pocket PC applications require input from the user, and the usual way to enter data is the Soft Input Panel (SIP), which supports several input methods. The standard SIP enables you to select characters from a virtual keyboard or write freehand characters and even words that are recognized. There are also a number of third-party products available for other input methods (for example, using the full screen as a virtual keyboard that can be manipulated with your fingers).

As many mobile applications require input when being used in the field, it is important that your applications take the SIP into consideration. The first consequence of using the SIP is that it will partially cover your application's forms when visible. If your user interface does not react to the SIP, it means that some of your controls may be hidden, and therefore input into these controls is not possible. Users are not comfortable about managing the visibility of the SIP manually. They expect the SIP to be made visible when an input field is selected.

The InputPanel Control

The InputPanel control included with Microsoft® Visual Studio® .NET 2003 is the way to interface with the SIP on a Pocket PC. The implementation of the control is straightforward, with just one property (Enabled) that controls the visibility of the SIP and one event (EnabledChanged) that is fired when the visibility of the SIP changes.

Let's see how the InputPanel control can be used in a sample Pocket PC application.

InputPanel Sample

This is a sample application for the Pocket PC created with Visual Studio .NET 2003, C#, and the Microsoft .NET Compact Framework. It shows how to implement the InputPanel control in a tabbed form and consists of one form:

\

Figure 1. InputPanel sample

The form has a tab control with two pages holding a number of input fields. When a TextBox control receives the focus, the SIP is enabled (shown). When the TextBox looses the focus the SIP is disabled (hidden). A very important detail is that the tab control is resized when the SIP is shown to enable the user to select different tabs even when the SIP is enabled.

The design of the form has taken the SIP into consideration because no controls are hidden when the SIP is enabled. A special case is the ComboBox control that does not require any input, and therefore doesnÂ’t require the SIP. To maximize the use of the form space, such controls can be placed in the lower area of the form, as the SIP will be removed when the control receives the focus:

\

Figure 2. SIP removed when ComboBox has focus

As the above figure shows, the SIP is removed when the ComboBox has the focus. However, if possible, a perfect design should allow all controls to be fully visible when the SIP is shown.

Now, let's take a look at how this form is implemented.

Code Walkthrough

In the sample code, the InputPanel control is named inpSIP. To enable each of the TextBox controls to enable (show) and disable (hide) the SIP control, depending on if they have the focus, the following event handlers are created:

private void txt_GotFocus(object sender, System.EventArgs e)
{
  inpSIP.Enabled = true;
}
private void txt_LostFocus(object sender, System.EventArgs e)
{
  inpSIP.Enabled = false;
}

As they are very generic, the same event handlers can be attached to all the TextBox controls in the form. When attaching the event handlers to each TextBox, the code generated looks like this:

txtFirst.GotFocus += new System.EventHandler(this.txt_GotFocus);
txtFirst.LostFocus += new System.EventHandler(this.txt_LostFocus);
txtSecond.GotFocus += new System.EventHandler(this.txt_GotFocus);
txtSecond.LostFocus += new System.EventHandler(this.txt_LostFocus);
// and so on

The only issue with this approach is when you close the form, the Enabled property of the InputPanel control may not be available when the LostFocus event is fired on the TextBox that has the focus. One simple solution to handle this is to set the focus to a non-TextBox control in the Closing event of the form:

private void frmMain_Closing(object sender, CancelEventArgs e)
{
  cboSixth.Focus();
}

In this case the focus is set to the ComboBox control when the form is closed.

The final issue to solve is the resizing of the TabControl when the SIP is enabled (visible). The code to do this looks like this:

private void inpSIP_EnabledChanged(object sender, System.EventArgs e)
{
  if (inpSIP.Enabled)
  {
    tabControl.Height = 246 - inpSIP.Bounds.Height;
  }
  else
  {
    tabControl.Height = 246;
  }
}

The EnabledChanged event of the InputPanel control is fired each time the SIP state (enabled/disabled) changes. This enables us to change the size of the TabControl control. The Bounds property of the InputPanel control holds the rectangle of the SIP when it is visible.

Conclusion

With simple code, your user interface can handle the SIP in an efficient way using the InputPanel control. Your code can support showing the SIP control when required (when TextBox controls have focus). It can also handle the dynamic changes to the user interface (resize of TabControl controls) with a minimum of code.

關(guān)鍵詞:PPC