Add Tap-and-Hold Support in Windows Mobile-based Pocket PC Applications

 

Christian Forsberg
businessanyplace.net

September 2003

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

Summary: Learn how to add tap-and-hold support using context menus to the most common controls in your Windows Mobile-based Pocket PC applications. (6 printed pages)

Download tapnhold_support.exe

Contents

Tap-and-Hold Everywhere
The ContextMenu Control
Tap-and-Hold Sample
Code Walkthrough
Conclusion

Tap-and-Hold Everywhere

Any Pocket PC application written to conform to the basic design guidelines has support for tap-and-hold operations in most controls. The most common functionality is to allow typical edit menu options (cut, copy, paste, and so on) on input fields. The next most common thing is probably to offer list manipulation options (new, edit, and delete). A key issue is the context sensitive menus that appear after a tap-and-hold operation is performed.

The reason for tap-and-hold and context menus is that it makes the user interface more efficient. If the user doesn't have to move the stylus down to the menu to perform an action, valuable time is saved. More importantly, the user will remain focused on the data that is being manipulated.

The ContextMenu Control

The ContextMenu control included with Microsoft® Visual Studio® .NET 2003 is the tool to use when adding tap-and-hold functionality to controls. It works in a similar way to the MainMenu control that you probably already use in all forms. Most controls have support for linking to a ContextMenu control through a ContextMenu property. When this property is set, a tap-and-hold operation with automatically bring up the context menu for the control.

Let's see how tap-and-hold and context menus can be used in a sample Pocket PC application.

Tap-and-Hold 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 add tap-and-hold support using context menus to the most common controls. The application consists of one form:

Figure 1. Context menu with TextBox control

The sample has tap-and-hold support on an input field (TextBox) and a list (ListView). The context menu that appears when you tap-and-hold on the input field and enables basic edit menu options like Cut, Copy, Paste, and Delete. Each option will always act on all text in the field. Any selection in the input field will be removed as soon as the context menu appears. The context menu options will be enabled depending on whether they are possible to perform (for instance, you cannot paste if you don't have anything on the clipboard).

Figure 2. Context menu with ListView control

When you tap-and-hold on the list you get another context menu with common options for editing lists like New, Edit, and Delete. When you select the New option, the current content of the sample input field will be added to the list, and when you select Edit, the list content will be copied to the input field. Finally, when the Delete option is selected, the row is removed after confirmation is made in a message box. The New option can be done from anywhere in the list, but to do an edit and delete, a row in the list must be selected.

Code Walkthrough

The edit menu functionality for the TextBox uses a simple clipboard simulator—actually just a string variable declared in the form:

private string simpleClipboard = "";

This means that the edit operations performed in the sample will not be available to other applications. The solution is of course to interact with the real clipboard using some Windows API calls, but that is not really the purpose of this article.

The ContextMenu control and its options are easily created using the design support built into the control. The code for the options in the context menu linked to the TextBox control looks like this:

private void mitTextCut_Click(object sender, System.EventArgs e)
{
  simpleClipboard = txtSample.Text;
  txtSample.Text = "";
}

private void mitTextCopy_Click(object sender, System.EventArgs e)
{
  simpleClipboard = txtSample.Text;    
}

private void mitTextPaste_Click(object sender, System.EventArgs e)
{
  txtSample.Text = simpleClipboard;
}

private void mitTextDelete_Click(object sender, System.EventArgs e)
{
  txtSample.Text = "";
}

Note that the menu options will always act on all of the text in the TextBox control. Ideally, the TextBox would keep the current selection when the context menu appears, but that is not the way it currently works.

Just before the ContextMenu control appears, an event, if fired, makes it possible to modify the menu options depending on the current state. The code for this event looks like this:

private void cmnText_Popup(object sender, System.EventArgs e)
{
  mitTextCut.Enabled = (txtSample.TextLength > 0);
  mitTextCopy.Enabled = mitTextCut.Enabled;
  mitTextDelete.Enabled = mitTextCut.Enabled;
  mitTextPaste.Enabled = (simpleClipboard.Length > 0);
}

If there is any text in the TextBox, the options that require data (Cut, Copy, and Delete) are enabled. Paste is only enabled if there is text in the "clipboard".

The code for the options in the ListView control's context menu looks like this:

private void mitListNew_Click(object sender, System.EventArgs e)
{
  ListViewItem lit = new ListViewItem(txtSample.Text);
  lvwSample.Items.Add(lit);
}

private void mitListEdit_Click(object sender, System.EventArgs e)
{
  if (lvwSample.SelectedIndices.Count > 0)
    txtSample.Text = lvwSample.Items[lvwSample.SelectedIndices[0]].Text;
  else
    MessageBox.Show("Select item in list to edit!", this.Text);
}

private void mitListDelete_Click(object sender, System.EventArgs e)
{
  if (lvwSample.SelectedIndices.Count > 0)
  {
    if (MessageBox.Show("Delete sample " + 
        lvwSample.Items[lvwSample.SelectedIndices[0]].Text + "?", 
          this.Text,
        MessageBoxButtons.YesNo, MessageBoxIcon.Question,
        MessageBoxDefaultButton.Button2) == DialogResult.Yes)
      lvwSample.Items.Remove(lvwSample.Items
        [lvwSample.SelectedIndices[0]]);
  }
  else
    MessageBox.Show("Select item in list to delete!", this.Text);
}

The ListView control in combination with tap-and-hold and context menu functionality is something that is common to most enterprise Pocket PC applications. The above code can probably be used as a template for many ListView implementations using tap-and-hold and context menus.

Conclusion

The support for tap-and-hold and context menus functionality enables the creation of a very efficient user interface in your Pocket PC applications. As always, the support can be improved, but the current support is sufficient for most needs.