Freigeben über


RecognitionAlternates.CopyTo Method

RecognitionAlternates.CopyTo Method

Copies all of the elements of the current RecognitionAlternates collection to the specified one-dimensional array, starting at the specified destination array index.

Definition

Visual Basic .NET Public Sub CopyTo( _
ByVal array As Array, _
ByVal index As Integer _
)
C# public void CopyTo(
Array array,
int index
);
Managed C++ public: void CopyTo(
Array *array,
int *index
);

Parameters

array System.Array. The one-dimensional array that is the destination of elements copied from the collection. The array must have zero-based indexing.
index System.Int32. The zero-based index in the array parameter at which copying begins.

Remarks

The elements are copied to the Array Leave Site object in the same order in which the enumerator iterates through the RecognitionAlternates.

An exception is thrown if:

  • The array parameter is a null (Nothing in Microsoft® Visual Basic® .NET) reference.
  • The index parameter is less than zero.
  • The array parameter is multidimensional.
  • The index parameter is equal to or greater than the length of the array parameter.
  • The number of elements in the collection is higher than the available space from the index parameter to the end of the destination array parameter.

The type of the collection is cast automatically to the type of the destination array parameter. However, this method only copies elements to arrays of the same type as the elements of the collection or arrays of type Object Leave Site. Attempting to copy to another type of array causes an InvalidCastException Leave Site exceptionto be thrown.

This method is synchronized.

Examples

[C#]

This C# example uses the CopyTo method to fill an array of RecognitionAlternate objects with the entire contents of the RecognitionAlternates collection from theInkCollector.Ink.Strokes, starting at element 0 (the first element in a zero-based array) of the destination array, theCopies.

public void Form1_Load(object sender, System.EventArgs e)
{
        
    //Create and enable an InkCollector and a RecognizerContext.
    theInkCollector = new InkCollector(Handle);
    theInkCollector.Enabled = true;
    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.RecognitionWithAlternates += new
        RecognizerContextRecognitionWithAlternatesEventHandler(RecognitionWithAlternates_Event);
}

// Stroke event handler
public 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.BackgroundRecognizeWithAlternates();
}

// Recognition Event Handler
public void RecognitionWithAlternates_Event(object sender,
    RecognizerContextRecognitionWithAlternatesEventArgs e)
{
    // Create a RecognitionResult and add the recognition results to it.
    RecognitionResult theRecognitionResult;
    theRecognitionResult = e.Result;

    //Create a RecognitionAlternates collection
    RecognitionAlternates theRecognitionAlternates =
        theRecognitionResult.GetAlternatesFromSelection(0, -1);

    //Create the array to copy alternates into
    RecognitionAlternate [] theCopies = new RecognitionAlternate[theRecognitionAlternates.Count];

    //Copy the alternates to the array
    theRecognitionAlternates.CopyTo(theCopies, 0);

    //Do something with the array, such as
    //add each alternate to a listbox
    theListBox.Items.Clear();
    for (int i=0; i<theCopies.GetUpperBound(0); i++)
    {
        theListBox.Items.Add(theCopies[i].ToString());
    }

}

[Visual Basic .NET]

This Visual Basic .NET example uses the CopyTo method to fill an array of RecognitionAlternate objects with the entire contents of the RecognitionAlternates collection from theInkCollector.Ink.Strokes, starting at element 0 (the first element in a zero-based array) of the destination array, theCopies.

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.RecognitionWithAlternates, _
        AddressOf RecognizeWithAlternates_Event
        'Enable the ink collector
        theInkCollector.Enabled = True
    End Sub

    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.BackgroundRecognizeWithAlternates()
    End Sub

    Private Sub RecognizeWithAlternates_Event( _
    ByVal sender As Object, _
    ByVal e As RecognizerContextRecognitionWithAlternatesEventArgs)

        'Create RecognitionAlternates collection
        theRecognitionResult = e.Result
        Dim theRecognitionAlternates As RecognitionAlternates
        theRecognitionAlternates = theRecognitionResult.GetAlternatesFromSelection(0, -1)

        'Create the array to copy alternates into
        Dim theCopies() As RecognitionAlternate
        ReDim theCopies(theRecognitionAlternates.Count)

        'Copy the alternates into the array
        theRecognitionAlternates.CopyTo(theCopies, 0)

        'Do something with the array, such as
        'add each alternate to a listbox.
        theListbox.Items.Clear()
        Dim i As Integer
        For i = 0 To theCopies.GetUpperBound(0)
            theListbox.Items.Add(theCopies(i).ToString())
        Next
    End Sub