Freigeben über


RecognizerContext.Recognition Event

Occurs when the RecognizerContext object has generated results from the BackgroundRecognize method.

Namespace: Microsoft.Ink
Assembly: Microsoft.Ink (in microsoft.ink.dll)

Syntax

'Declaration
Public Event Recognition As RecognizerContextRecognitionEventHandler
'Usage
Dim instance As RecognizerContext
Dim handler As RecognizerContextRecognitionEventHandler

AddHandler instance.Recognition, handler
public event RecognizerContextRecognitionEventHandler Recognition
public:
event RecognizerContextRecognitionEventHandler^ Recognition {
    void add (RecognizerContextRecognitionEventHandler^ value);
    void remove (RecognizerContextRecognitionEventHandler^ value);
}
/** @event */
public void add_Recognition (RecognizerContextRecognitionEventHandler value)

/** @event */
public void remove_Recognition (RecognizerContextRecognitionEventHandler value)
In JScript, you can handle the events defined by a class, but you cannot define your own.
Not applicable.

Remarks

The event handler receives an argument of type RecognizerContextRecognitionEventArgs that contains data about this event.

When you create a RecognizerContextRecognitionEventHandler delegate, you identify the method that handles the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate.

The behavior of the application programming interface (API) is unpredictable if you try to gain access to the original RecognizerContext object from the recognition event handler. Do not attempt to do this. Instead, if you need to do this, create a flag and set it in the Recognition event handler. Then you can poll that flag to determine when to change the RecognizerContext properties outside of the event handler.

Example

This C# example tracks each stroke made in an InkCollector object, theInkCollector, and calls the BackgroundRecognize method, which subsequently causes a Recognition event to occur. The results of the recognition appear in the text box, theTextBox.

using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.Ink;

class RecognitionEventForm : System.Windows.Forms.Form
{
    private TextBox theTextBox;
    InkCollector theInkCollector;
    Strokes theStrokes;
    RecognizerContext theRecognizerContext;

    public RecognitionEventForm()
    {
        // Initialize the form and the textbox.
        // Call SuspendLayout while we adjust things.
        this.theTextBox = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        this.theTextBox.Dock = System.Windows.Forms.DockStyle.Bottom;
        this.theTextBox.Location = new System.Drawing.Point(0, 300);
        this.theTextBox.Size = new System.Drawing.Size(350, 20);
        this.theTextBox.Text = "";
        this.ClientSize = new System.Drawing.Size(350, 320);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.theTextBox});
        this.Text = "Recognition Event Demo";
        this.ResumeLayout(false);

        // Create new ink collector and recognizer context
        theInkCollector = new InkCollector(Handle);
        theRecognizerContext = new RecognizerContext();

        // Initialize the recognizer's strokes
        // and assign them to the recognizer context.
        theStrokes = theInkCollector.Ink.Strokes;
        theRecognizerContext.Strokes = theStrokes;

        // Install event handlers.
        theInkCollector.Stroke +=
            new InkCollectorStrokeEventHandler(Stroke_Event);
        theRecognizerContext.Recognition +=
            new RecognizerContextRecognitionEventHandler(Recognition_Event);

        // Enable the ink collector
        theInkCollector.Enabled = true;
    }

    public static void Main()
    {
        Application.Run(new RecognitionEventForm());
    }

    // Stroke event handler
    private void Stroke_Event(object sender,
        InkCollectorStrokeEventArgs e)
    {
        // When a new stroke is collected,
        // add it to the recognizer's strokes collection.
        theStrokes.Add(e.Stroke);
        // Tell the context to recognize its strokes.
        theRecognizerContext.BackgroundRecognize();
    }

    // Recognition Event Handler
    private void Recognition_Event(object sender,
        RecognizerContextRecognitionEventArgs e)
    {
        // Update the Text box with the top string of the result
        theTextBox.Text = e.Text;
    }

        // Event handler for the form's closed event
        private void RecognitionEventForm_Closed(object sender, System.EventArgs e)
        {
            theInkCollector.Dispose();
            theInkCollector = null;
            theRecognizerContext.Dispose();
            theRecognizerContext = null;
        }
}

This Microsoft® Visual Basic® .NET example tracks each stroke made in an InkCollector object, theInkCollector, and calls the BackgroundRecognize method, which subsequently causes a Recognition event to occur. The results of the recognition appear in the text box, theTextBox.

Imports Microsoft.Ink
Public Class Form1
    Inherits System.Windows.Forms.Form

'... Code to create Form1 and theTextBox goes here.

    Dim theInkCollector As InkCollector
    Dim theStrokes As Strokes
    Dim theRecognizerContext As RecognizerContext
    Dim theRecognitionResult As RecognitionResult

    Private Sub Stroke_Event(ByVal sender As Object, _
    ByVal e As InkCollectorStrokeEventArgs)
        'When a new stroke is collected, add it to
        'the RecognizerContext's strokes collection
        theStrokes.Add(e.Stroke)
        'Tell the RecognizerContext to recognize
        theRecognizerContext.BackgroundRecognize()
    End Sub

    Private Sub Recognize_Event( _
    ByVal sender As Object, _
    ByVal e As RecognizerContextRecognitionEventArgs)
        'Update the Text box with the top result
        theTextBox.Text = e.Text
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        theInkCollector = New InkCollector(Handle)
        'Create new RecognizerContext
        theRecognizerContext = New RecognizerContext()
        'Initialize the recognizer's strokes
        'and assign them to the RecognizerContext
        theStrokes = theInkCollector.Ink.Strokes
        theRecognizerContext.Strokes = theStrokes
        'Add the handler for the Stroke event
        AddHandler theInkCollector.Stroke, AddressOf Stroke_Event
        'Handle Recognition event
        AddHandler theRecognizerContext.Recognition, _
        AddressOf Recognize_Event
        'Enable the ink collector
        theInkCollector.Enabled = True
    End Sub

    ' Event handler for the form's closed event
    Private Sub Form1_Closed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Closed
        theInkCollector.Dispose()
        Set theInkCollector = Nothing
        theRecognizerContext.Dispose()
        Set theRecognizerContext = Nothing
    End Sub
End Class

Platforms

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

Version Information

.NET Framework

Supported in: 3.0

See Also

Reference

RecognizerContext Class
RecognizerContext Members
Microsoft.Ink Namespace
RecognitionResult
RecognitionStatus
Microsoft.Ink.RecognizerContext.BackgroundRecognize
RecognizerContext.Recognize