Dela via


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.

    Me.KeyPreview = True 
    Me.AutoScroll = True
    
    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.

    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
    
    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if ((e.KeyCode == System.Windows.Forms.Keys.Up))
        {
            this.AutoScrollPosition = new Point(0, -this.AutoScrollPosition.Y - 16);
        }
        if ((e.KeyCode == System.Windows.Forms.Keys.Down))
        {
            this.AutoScrollPosition = new Point(0, -this.AutoScrollPosition.Y + 16);
        }
    

Compiling the Code

This example requires references to the following namespaces:

See Also

Concepts

.NET Compact Framework How-to Topics

Other Resources

Smartphone Development and the .NET Compact Framework