20070611 .NET Framework Developer\’s Guide how to
http://www.yippeesoft.com
.NET Framework Developer\’s Guide
How to: Use Smartphone Menus
To conform to the Smartphone user interface, the .NET Compact Framework enforces the following menu restrictions:
*
You can only have two top-level menu items.
*
Only the second top-level menu item, on the right side of the form, can have submenus.
Note that the .NET Compact Framework does not enforce these restrictions at design time, but does throw a NotSupportedException at run time if your code does not follow them.
At run time, you cannot delete a top-level menu item. However, you can set Enabled property of a MenuItem to an empty string ("") to make a menu item appear invisible.
Visual Studio automatically adds a MainMenu component to your form when you create Smartphone and Pocket PC applications, but does not add it to child forms. The MainMenu component operates the Smartphone soft keys, but you cannot program their functionality unless you remove the MainMenu component from the form. For more information about programming soft keys, see Using Smartphone Back Key and Soft Keys.
To associate a method with a menu selection, provide code for the Click event for a MenuItem.
Example
This example defines a menu system for a scenario of selecting maps:
*
On the left is the Map Help menu item, which has event handling code that displays a message box.
*
On the right is the Maps menu item, which has two children: My Maps and Add and Remove. These children have, respectively, five and two children of their own.
Visual Basic
Imports System Imports System.Windows.Forms Public Class Form1 Inherits System.Windows.Forms.Form Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Private WithEvents mi1 As New MenuItem Private mi2 As New MenuItem Private miChildA As New MenuItem Private miChildB As New MenuItem Private WithEvents miGrandChildA1 As New MenuItem Private WithEvents miGrandChildA2 As New MenuItem Private WithEvents miGrandChildA3 As New MenuItem Private WithEvents miGrandChildA4 As New MenuItem Private WithEvents miGrandChildA5 As New MenuItem Private WithEvents miGrandChildB1 As New MenuItem Private WithEvents miGrandChildB2 As New MenuItem Public Sub New() MyBase.New() InitializeComponent() \’Define and add menu items. MainMenu1.MenuItems.Add(mi1) MainMenu1.MenuItems.Add(mi2) mi2.MenuItems.Add(miChildA) mi2.MenuItems.Add(miChildB) miChildA.MenuItems.Add(miGrandChildA1) miChildA.MenuItems.Add(miGrandChildA2) miChildA.MenuItems.Add(miGrandChildA3) miChildA.MenuItems.Add(miGrandChildA4) miChildA.MenuItems.Add(miGrandChildA5) miChildB.MenuItems.Add(miGrandChildB1) miChildB.MenuItems.Add(miGrandChildB2) mi1.Text = "Map Help" mi2.Text = "Maps" miChildA.Text = "My Maps" miChildB.Text = "Add and remove" miGrandChildA1.Text = "Manhattan" miGrandChildA2.Text = "Bronx" miGrandChildA3.Text = "Brooklyn" miGrandChildA4.Text = "Queens" miGrandChildA5.Text = "Staten Island" miGrandChildB1.Text = "Add map" miGrandChildB2.Text = "Delete map" End Sub Public Shared Sub Main() Application.Run(New Form1) End Sub \’Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) MyBase.Dispose(disposing) End Sub Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.Menu = Me.MainMenu1 Me.Text = "Form1" End Sub \’ The following subroutine handles the \’ Click event for the mi1 MenuItem. Private Sub mi1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mi1.Click MessageBox.Show("This is just a test.") End Sub End Class
C#
using System; using System.Windows.Forms; namespace SmartphoneMenus &leftsign; public class Form1 : System.Windows.Forms.Form &leftsign; private System.Windows.Forms.MainMenu mainMenu1; private MenuItem mi1 = new MenuItem(); private MenuItem mi2 = new MenuItem(); private MenuItem miChildA = new MenuItem(); private MenuItem miChildB = new MenuItem(); private MenuItem miGrandChildA1 = new MenuItem(); private MenuItem miGrandChildA2 = new MenuItem(); private MenuItem miGrandChildA3 = new MenuItem(); private MenuItem miGrandChildA4 = new MenuItem(); private MenuItem miGrandChildA5 = new MenuItem(); private MenuItem miGrandChildB1 = new MenuItem(); private MenuItem miGrandChildB2 = new MenuItem(); public Form1() &leftsign; InitializeComponent(); mainMenu1.MenuItems.Add(mi1); mainMenu1.MenuItems.Add(mi2); mi2.MenuItems.Add(miChildA); mi2.MenuItems.Add(miChildB); miChildA.MenuItems.Add(miGrandChildA1); miChildA.MenuItems.Add(miGrandChildA2); miChildA.MenuItems.Add(miGrandChildA3); miChildA.MenuItems.Add(miGrandChildA4); miChildA.MenuItems.Add(miGrandChildA5); miChildB.MenuItems.Add(miGrandChildB1); miChildB.MenuItems.Add(miGrandChildB2); // Event handler for the top left menu. mi1.Click +=new EventHandler(mi1_Click); // Event handlers for grandchild menu items. This code is commented out // because this example does not define their event handling methods. // miGrandChildA1.Click +=new EventHandler(miGrandChildA1_Click); // miGrandChildB1.Click +=new EventHandler(miGrandChildB1_Click); // miGrandChildB2.Click +=new EventHandler(miGrandChildB2_Click); mi1.Text = "Map Help"; mi2.Text = "Maps"; miChildA.Text = "My Maps"; miChildB.Text = "Add and remove"; miGrandChildA1.Text = "Manhattan"; miGrandChildA2.Text = "Bronx"; miGrandChildA3.Text = "Brooklyn"; miGrandChildA4.Text = "Queens"; miGrandChildA5.Text = "Staten Island"; miGrandChildB1.Text = "Add map"; miGrandChildB2.Text = "Remove map"; &rightsign; protected override void Dispose( bool disposing ) &leftsign; base.Dispose( disposing ); &rightsign; private void InitializeComponent() &leftsign; this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.Menu = this.mainMenu1; this.Text = "Form1"; &rightsign; static void Main() &leftsign; Application.Run(new Form1()); &rightsign; // The following method handles the // Click event for the mi1 MenuItem. private void mi1_Click(object sender, EventArgs e) &leftsign; MessageBox.Show("This is just a test."); &rightsign; &rightsign; &rightsign;
Compiling the Code
This example requires references to the following namespaces:
*
System
*
System.Windows.Forms
How to: Set Smartphone Input Modes
You can set the input mode for a TextBox in a Smartphone application to ABC, T9, and numeric input modes as defined by the InputMode enumeration. The InputModeEditor class provides access to Smartphone input methods for entering text.
The AlphaCurrent mode is the preferred input mode value for text boxes used for alpha characters. This mode matches the mode selected by holding down the star (*) key on the Smartphone.
You cannot use InputModeEditor to explicitly change casing settings for alpha input modes. However, the alpha input mode used (T9 or ABC) are retained by the AlphaCurrent input mode when set with the star key.
You can only use InputModeEditor on a Smartphone, and only with a TextBox control.
Example
The following code example shows setting the input mode on three text boxes: Name, Phone, and City. The Name and City text boxes are set with the AlphaCurrent input mode and the Phone text box is set with the Numeric input mode.
To observe how AlphaCurrent works, perform the following procedure:
1.
With the Name text box selected, hold down the star key and enter text using either the T9 or ABC input modes.
2.
Enter text in the City text box. Note that the input mode is the same as the Name text box.
Visual Basic
Imports System Imports System.Windows.Forms Imports Microsoft.WindowsCE.Forms Public Class Form1 Inherits System.Windows.Forms.Form Private mainMenu1 As System.Windows.Forms.MainMenu Private mi1 As System.Windows.Forms.MenuItem \’ Text box for name. Private textBox1 As System.Windows.Forms.TextBox \’ Text box for phone number. Private textBox2 As System.Windows.Forms.TextBox \’ Text box for city. Private textBox3 As System.Windows.Forms.TextBox \’ Labels for name, phone, and city Private label1 As System.Windows.Forms.Label Private label2 As System.Windows.Forms.Label Private label3 As System.Windows.Forms.Label Public Sub New() InitializeComponent() \’ Add a menu to close the application. mi1 = New MenuItem() mainMenu1.MenuItems.Add(mi1) AddHandler mi1.Click, AddressOf mi1_Click mi1.Text = "Done" \’ Set input mode for name text box to AlphaCurrent. InputModeEditor.SetInputMode(textBox1, InputMode.AlphaCurrent) \’ Set input mode for phone number text box to Numeric. InputModeEditor.SetInputMode(textBox2, InputMode.Numeric) \’ Set input mode for city text box to AlphaCurrent. InputModeEditor.SetInputMode(textBox3, InputMode.AlphaCurrent) End Sub Protected Overrides Sub Dispose(disposing As Boolean) MyBase.Dispose(disposing) End Sub Private Sub InitializeComponent() Me.mainMenu1 = New System.Windows.Forms.MainMenu() Me.mainMenu1 = New System.Windows.Forms.MainMenu() Me.textBox1 = New System.Windows.Forms.TextBox() Me.textBox2 = New System.Windows.Forms.TextBox() Me.textBox3 = New System.Windows.Forms.TextBox() Me.label1 = New System.Windows.Forms.Label() Me.label2 = New System.Windows.Forms.Label() Me.label3 = New System.Windows.Forms.Label() \’ \’ textBox1 \’ Me.textBox1.Location = New System.Drawing.Point(64, 8) Me.textBox1.Size = New System.Drawing.Size(104, 25) Me.textBox1.Text = "" \’ \’ textBox2 \’ Me.textBox2.Location = New System.Drawing.Point(64, 40) Me.textBox2.Size = New System.Drawing.Size(104, 25) Me.textBox2.Text = "" \’ \’ textBox3 \’ Me.textBox3.Location = New System.Drawing.Point(64, 72) Me.textBox3.Size = New System.Drawing.Size(104, 25) Me.textBox3.Text = "" \’ \’ label1 \’ Me.label1.Location = New System.Drawing.Point(8, 8) Me.label1.Size = New System.Drawing.Size(56, 22) Me.label1.Text = "Name" \’ \’ label2 \’ Me.label2.Location = New System.Drawing.Point(8, 40) Me.label2.Size = New System.Drawing.Size(56, 22) Me.label2.Text = "Phone" \’ \’ label3 \’ Me.label3.Location = New System.Drawing.Point(8, 72) Me.label3.Size = New System.Drawing.Size(56, 22) Me.label3.Text = "City" \’ \’ Form1 \’ Me.Controls.Add(textBox1) Me.Controls.Add(textBox2) Me.Controls.Add(textBox3) Me.Controls.Add(label1) Me.Controls.Add(label2) Me.Controls.Add(label3) Me.Menu = Me.mainMenu1 Me.Text = "Input Mode Demo" End Sub Shared Sub Main() Application.Run(New Form1()) End Sub Private Sub mi1_Click(sender As Object, e As EventArgs) Me.Close() End Sub End Class
C#
using System; using System.Drawing; using System.Collections; using System.Windows.Forms; using Microsoft.WindowsCE.Forms; public class Form1 : System.Windows.Forms.Form &leftsign; private System.Windows.Forms.MainMenu mainMenu1; private System.Windows.Forms.MenuItem mi1; // Text box for name. private System.Windows.Forms.TextBox textBox1; // Text box for phone number. private System.Windows.Forms.TextBox textBox2; // Text box for city. private System.Windows.Forms.TextBox textBox3; // Labels for name, phone, and city private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; public Form1() &leftsign; InitializeComponent(); // Add a menu to close the application. mi1 = new MenuItem(); mainMenu1.MenuItems.Add(mi1); mi1.Click +=new EventHandler(mi1_Click); mi1.Text = "Done"; // Set input mode for name text box to AlphaCurrent. InputModeEditor.SetInputMode(textBox1, InputMode.AlphaCurrent); // Set input mode for phone number text box to Numeric. InputModeEditor.SetInputMode(textBox2, InputMode.Numeric); // Set input mode for city text box to AlphaCurrent. InputModeEditor.SetInputMode(textBox3, InputMode.AlphaCurrent); &rightsign; protected override void Dispose( bool disposing ) &leftsign; base.Dispose( disposing ); &rightsign; private void InitializeComponent() &leftsign; this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.mainMenu1 = new System.Windows.Forms.MainMenu(); this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(64, 8); this.textBox1.Size = new System.Drawing.Size(104, 25); this.textBox1.Text = ""; // // textBox2 // this.textBox2.Location = new System.Drawing.Point(64, 40); this.textBox2.Size = new System.Drawing.Size(104, 25); this.textBox2.Text = ""; // // textBox3 // this.textBox3.Location = new System.Drawing.Point(64, 72); this.textBox3.Size = new System.Drawing.Size(104, 25); this.textBox3.Text = ""; // // label1 // this.label1.Location = new System.Drawing.Point(8, 8); this.label1.Size = new System.Drawing.Size(56, 22); this.label1.Text = "Name"; // // label2 // this.label2.Location = new System.Drawing.Point(8, 40); this.label2.Size = new System.Drawing.Size(56, 22); this.label2.Text = "Phone"; // // label3 // this.label3.Location = new System.Drawing.Point(8, 72); this.label3.Size = new System.Drawing.Size(56, 22); this.label3.Text = "City"; // // Form1 // this.Controls.Add(this.textBox1); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox3); this.Controls.Add(this.label1); this.Controls.Add(this.label2); this.Controls.Add(this.label3); this.Menu = this.mainMenu1; this.Text = "Input Mode Demo"; &rightsign; static void Main() &leftsign; Application.Run(new Form1()); &rightsign; private void mi1_Click(object sender, EventArgs e) &leftsign; this.Close(); &rightsign; &rightsign;
Compiling the Code
This example requires references to the following namespaces:
*
System
*
System.Windows.Forms
How to: Scroll a Form of Labels
Because a Label control does not receive the focus and does not support tabbing, a Smartphone application of only Label controls does not allow the user to navigate to labels beyond the visible client area of the form. The user of a Pocket PC application can tap the scroll bars to navigate, but this capability is not available on the Smartphone.
You can implement navigation by providing code in the event handler for the KeyDown event that adjusts the AutoScrollPosition property.
To scroll a form of Label controls
1.
Add several Label controls to the form so that some are below the visible client area. Use arrow keys in the Microsoft Visual Studio 2005 designer or write initialization code to position them.
2.
In the form\’s constructor, set the KeyPreview and AutoScroll properties to true. C# users must attach a delegate for the KeyDown event handler.
Visual Basic
Me.KeyPreview = True Me.AutoScroll = True
C#
this.KeyPreview = true; this.KeyDown += new KeyEventHandler(Form1_KeyDown); this.AutoScroll = true;
3.
Set the AutoScrollPosition property to move vertically by a set number of pixels for the y point coordinate. The following code example uses 16. Note that the code is complex because AutoScrollPosition is offset by negative values, but the provided point values must be expressed as positive.
Visual Basic
Private Sub Form1_KeyDown(ByVal Sender As System.Object, _ ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = System.Windows.Forms.Keys.Up Then Me.AutoScrollPosition = New Point(0, -Me.AutoScrollPosition.Y – 16) End If If e.KeyCode = System.Windows.Forms.Keys.Down Then Me.AutoScrollPosition = New Point(0, -Me.AutoScrollPosition.Y + 16) End If End Sub
C#
private void Form1_KeyDown(object sender, KeyEventArgs e) &leftsign; if ((e.KeyCode == System.Windows.Forms.Keys.Up)) &leftsign; this.AutoScrollPosition = new Point(0, -this.AutoScrollPosition.Y – 16); &rightsign; if ((e.KeyCode == System.Windows.Forms.Keys.Down)) &leftsign; this.AutoScrollPosition = new Point(0, -this.AutoScrollPosition.Y + 16); &rightsign;
Compiling the Code
This example requires references to the following namespaces:
*
System
*
System.Windows.Forms
How to: Override the Smartphone Back Key
NoteNote
Note that back key functionality is critical for navigating between Smartphone applications. In most cases, it is contrary to Smartphone user interface guidelines to alter the default navigation behavior of the back key. Use discretion in determining when to override this functionality.
You can customize the back key in Smartphone applications, such as for a game. It operates according depending on the context of the key press, as described in the following table.
Back Key Operation Context
Cancels modal dialog boxes.
Always.
Cancels shortcut menus.
Always.
Performs a backspace operation.
When the focus is on an editable control, such as a text box, or on an editable custom control.
Navigates to the next window in the z-order.
Note that when the focus is on a form or custom control, the back key raises a KeyPress event that you can handle to provide your own functionality, as demonstrated in the example.
If you do not handle the event, the focus navigates to the next window in the z-order.
When the focus is on a form, non-editable control (such as a radio button), or non-editable custom control.
The back key operates the same way regardless of whether there is a menu bar. A menu bar exists if the form contains a MainMenu component.
Example
The following code example shows how to implement custom back key functionality. When the back key is pressed on a form or custom control, it raises the KeyPress event with the KeyChar value equal to the ESC key (27). In the event handling code, determine whether the ESC key value was raised. If it was, cancel the default back key operation by setting the Handled property to true. If the event arguments are not handled, the back key navigates to the next window in the z-order.
Visual C# users need to define an event hander for the KeyPress event in the form\’s constructor.
C#
// Connect an event handler to the KeyPress event this.KeyPress += new KeyPressEventHandler(OnKeyPress);
Visual Basic
Private Sub keypressed(ByVal o As [Object], _ ByVal e As KeyPressEventArgs) Handles MyBase.KeyPress \’ Determine if ESC key value is raised. If e.KeyChar = ChrW(Keys.Escape) Then \’ Handle the event to provide your own functionality. e.Handled = True \’ Add your event handling code here. MsgBox("Custom back key functionality.") End If End Sub
C#
private void OnKeyPress(object sender, KeyPressEventArgs ke) &leftsign; // Determine if ESC key value is pressed. if (ke.KeyChar == (Char)Keys.Escape) &leftsign; // Handle the event to provide functionality. ke.Handled = true; // Add your event handling code here. MessageBox.Show("Back key was pressed."); &rightsign; &rightsign;
Compiling the Code
This example requires references to the following namespaces:
*
System
*
System.Windows.Forms
历史博文
- Android模擬器 - 2010
- 20080707 firefox 配置文件 - 2009
- 0910 C# WIN VS vc++ 性能PK - 2007
- 0111 VS2005 发布 ClickOnce 测试 - 2006
- TestFixture Test TestDriven 测试驱动 - 2005
- 软件开发误区之四-开发语言工具之争 - 2005