--- title: "How to: Select Ink from a Custom Control" ms.date: "03/30/2017" dev_langs: - "csharp" - "vb" helpviewer_keywords: - "controls [WPF], ink selection" - "ink [WPF], selecting from custom control" - "custom controls [WPF], ink selection" ms.assetid: 5f3a45c6-6d40-4017-9b47-933f134ceba3 --- # How to: Select Ink from a Custom Control By adding an to your custom control, you can enable your control so that a user can select ink with a lasso tool, similar to the way the selects ink with a lasso. This example assumes you are familiar with creating an ink-enabled custom control. To create a custom control that accepts ink input, see [Creating an Ink Input Control](creating-an-ink-input-control.md). ## Example When the user draws a lasso, the predicts which strokes will be within the lasso path's boundaries after the user completes the lasso. Strokes that are determined to be within the lasso path's boundaries can be thought of as being selected. Selected strokes can also become unselected. For example, if the user reverses direction while drawing the lasso, the may unselect some strokes. The raises the event, which enables your custom control to respond while the user is drawing the lasso. For example, you can change the appearance of strokes as the user selects and unselects them. ## Managing the Ink Mode It is helpful to the user if the lasso appears differently than the ink on your control. To accomplish this, your custom control must keep track of whether the user is writing or selecting ink. The easiest way to do this is to declare an enumeration with two values: one to indicate that the user is writing ink and one to indicate that the user is selecting ink. [!code-csharp[HowToSelectInk#2](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#2)] [!code-vb[HowToSelectInk#2](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#2)] Next, add two to the class: one to use when the user writes ink, one to use when the user selects ink. In the constructor, initialize the and attach both events to the same event handler. Then set the property of the to the ink . [!code-csharp[HowToSelectInk#3](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#3)] [!code-vb[HowToSelectInk#3](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#3)] [!code-csharp[HowToSelectInk#4](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#4)] [!code-vb[HowToSelectInk#4](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#4)] Add a property that exposes the selection mode. When the user changes the selection mode, set the property of the to the appropriate object and then reattach the Property to the . [!code-csharp[HowToSelectInk#5](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#5)] [!code-vb[HowToSelectInk#5](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#5)] Expose the as properties so applications can determine the appearance of the ink strokes and selection strokes. [!code-csharp[HowToSelectInk#6](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#6)] [!code-vb[HowToSelectInk#6](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#6)] When a property of a object changes, the must be reattached to the . In the event handler for the event, reattach the to the . [!code-csharp[HowToSelectInk#7](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#7)] [!code-vb[HowToSelectInk#7](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#7)] ## Using the IncrementalLassoHitTester Create and initialize a that contains the selected strokes. [!code-csharp[HowToSelectInk#8](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#8)] [!code-vb[HowToSelectInk#8](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#8)] When the user starts to draw a stroke, either ink or the lasso, unselect any selected strokes. Then, if the user is drawing a lasso, create an by calling , subscribe to the event, and call . This code can be a separate method and called from the and methods. [!code-csharp[HowToSelectInk#9](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#9)] [!code-vb[HowToSelectInk#9](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#9)] Add the stylus points to the while the user draws the lasso. Call the following method from the , , , and methods. [!code-csharp[HowToSelectInk#10](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#10)] [!code-vb[HowToSelectInk#10](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#10)] Handle the event to respond when the user selects and unselects strokes. The class has the and properties that get the strokes that were selected and unselected, respectively. [!code-csharp[HowToSelectInk#11](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#11)] [!code-vb[HowToSelectInk#11](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#11)] When the user finishes drawing the lasso, unsubscribe from the event and call . [!code-csharp[HowToSelectInk#12](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#12)] [!code-vb[HowToSelectInk#12](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#12)] ## Putting it All Together. The following example is a custom control that enables a user to select ink with a lasso. [!code-csharp[HowToSelectInk#1](~/samples/snippets/csharp/VS_Snippets_Wpf/HowToSelectInk/CSharp/InkSelector.cs#1)] [!code-vb[HowToSelectInk#1](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HowToSelectInk/VisualBasic/InkSelector.vb#1)] ## See also - - - - [Creating an Ink Input Control](creating-an-ink-input-control.md)