Using an Application Dictionary with InkEdit

Using an Application Dictionary with InkEdit

Description of using an application dictionary with InkEdit.

In order to create an application dictionary, it is necessary to set the WordList property of the RecognizerContext object. The InkEdit control does not expose the RecognizerContext object, so it is not possible to directly set the WordList property of the InkEdit control's RecognizerContext object.

To use an Application Dictionary with an InkEdit control, you must take the strokes out of the InkEdit control, pass them to a new RecognizerContext object with the WordList property set to a WordList containing the application dictionary, store the results from this RecognizerContext object, and then pass the results back to the InkEdit control.

Example

The following C# code shows an example of this technique.

private RecognizerContext rc;
private WordList wl;
private int wlSelStart;
private int wlSelLen;
private void Form1_Load(object sender, System.EventArgs e)
{
   //event handler must be attached for dictionary to work.
   inkEdit1.Recognition += new Microsoft.Ink.InkEditRecognitionEventHandler(inkEdit1_Recognition);

   //create a WordList that contains the application dictionary
   wl = new WordList();
   //...
   //Add dictionary terms to the WordList
   //...

   // create a RecognizerContext for the WordList
   rc = inkEdit1.Recognizer.CreateRecognizerContext();
   rc.Factoid = Factoid.WordList;

   //set the RecognizerContext WordList to the newly created WordList
   rc.WordList = wl;
   //create a delegate for the new RecognizerContext
   rc.RecognitionWithAlternates += new
   RecognizerContextRecognitionWithAlternatesEventHandler(rc_Recognition);
}

private void inkEdit1_Recognition(object sender,
     Microsoft.Ink.InkEditRecognitionEventArgs e)
{
   //store the start of the selection in wlSelStart
   wlSelStart = inkEdit1.SelectionStart;

   //store the length of the selection in wlSelLen
   wlSelLen = e.RecognitionResult.TopString.Length;

   //copy the strokes from the InkEdit control into the new
   // RecognizerContext
   rc.Strokes = e.RecognitionResult.Strokes.Ink.Clone().Strokes;
   rc.BackgroundRecognizeWithAlternates();
}

private void rc_Recognition(object sender,
     Microsoft.Ink.RecognizerContextRecognitionWithAlternatesEventArgs e)
{
   //set the insertion point in the InkEdit control
   inkEdit1.SelectionStart = wlSelStart;
   inkEdit1.SelectionLength = wlSelLen;
   //insert the result from the new RecognizerContext
   //into the InkEdit control
   inkEdit1.SelectedText = e.Result.TopString;
   inkEdit1.SelectionStart = inkEdit1.SelectionStart +
                               e.Result.TopString.Length;
}

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