Version

Selection (xamSyntaxEditor)

Topic Overview

Purpose

This topic explains the programmatic use of the xamSyntaxEditor™ control’s selection feature.

Required background

The following topics are prerequisites to understanding this topic:

Topic Purpose

In this topic, you will find information to help you better understand the xamSyntaxEditor’s functions.

This topic provides you with systematic instructions designed to help you get the xamSyntaxEditor up and running as quickly as possible.

This topic covers the text editing capabilities of the xamSyntaxEditor control from both the developer and user’s perspective.

Introduction

Selection summary

The selection feature enables users to select contiguous parts of a document’s text, for manipulation, and identifies the selection by highlighting it with a different background color. You can programmatically select text using the SelectionManager property of the EditorDocumentView class. The SelectionManager property is of type ViewSelectionManager and can be used to:

  • Obtain the currently selected text

  • Replace the currently selected text

  • Obtain the start, end and length of the current selection

  • Manipulate the selected text using a large number of ViewSelectionManager methods

Working with Selection Summary

Selection configuration summary chart

The following table lists the configurable aspects of the Selection feature of the xamSyntaxEditor

Task Details Properties

Get the currently selected text

Get the length of current selection

Replace the current selection or insert text at caret’s current position

Get/Set the start for the current selection

Obtaining the Currently Selected Text

Overview

Use the SelectedText property to obtain the currently selected text.

Property settings

In order to: Use this property: And:

Get/Set the currently selected text

SelectedText

Set it to a variable of type String

Obtaining the Length of the Current Selection

Overview

The SelectionLength property returns an integer representing the number of characters in the current selection.

Property settings

In order to: Use this property: And:

Obtain the length of the current selection

SelectionLength

Set it to a variable of type Integer

Replacing the Currently Selected Text

Overview

To replace the currently selected text with new text, set the new text to the SelectedText property. If there is no text currently selected, the new text set to the SelectedText property is inserted at the current caret position.

Property settings

In order to: Use this property: And set the following arguments:

Replace current selection or insert text at the current caret position

SelectedText

  • New value of type string

Setting/Retrieving the Start and End Position of the Selection

Overview

You can set or obtain the selection’s start and end position using the SelectionStart and SelectionEnd properties. The values of these properties represent a zero-based start or end offset from the beginning of the TextDocumentSnapshot.

Property settings

In order to: Use this property: And set the following arguments:

Get/Set the start/end index of the current selection

SelectionStart

SelectionEnd

  • startTextLocation

  • endTextLocation

or

  • startSnapshotPoint

  • endSnapshotPoint

Code Examples

Code examples summary

The following table lists the code examples included in this topic.

Example Description

This example shows the programming behind selecting the text with a start index of 5 and an end index of 60.

This example shows how to get the length of currently selected text.

Code Example: Select Text Programmatically

Description

This example shows the programming behind selecting the text with a start index of 5 and an end index of 60.

Code

In C#:

int startIndex = 5;
int endIndex = 60;
TextDocumentSnapshot tdsp = this.mySyntaxEditor.ActiveDocumentView.CurrentSnapshot;
TextLocation stl = tdsp.LocationFromOffset(startIndex);
TextLocation etl = tdsp.LocationFromOffset(endIndex);
SnapshotPoint ssp = SnapshotPoint.FromTextLocation(stl, tdsp);
SnapshotPoint esp = SnapshotPoint.FromTextLocation(etl, tdsp);
this.mySyntaxEditor.ActiveDocumentView.SelectionManager.SelectionStart = ssp;
this.mySyntaxEditor.ActiveDocumentView.SelectionManager.SelectionEnd = esp;

In Visual Basic:

Dim startIndex As Integer = 5
Dim endIndex As Integer = 60
Dim tdsp As TextDocumentSnapshot = Me.mySyntaxEditor.ActiveDocumentView.CurrentSnapshot
Dim stl As TextLocation = tdsp.LocationFromOffset(startIndex)
Dim etl As TextLocation = tdsp.LocationFromOffset(endIndex)
Dim ssp As SnapshotPoint = SnapshotPoint.FromTextLocation(stl, tdsp)
Dim esp As SnapshotPoint = SnapshotPoint.FromTextLocation(etl, tdsp)
Me.mySyntaxEditor.ActiveDocumentView.SelectionManager.SelectionStart = ssp
Me.mySyntaxEditor.ActiveDocumentView.SelectionManager.SelectionEnd = esp

Code Example: Get the Length of a Selection

Description

This example shows how to get the length of the current text selection.

Code

In C#:

int lengthOfSelection = this.xamSyntaxEditor1.ActiveDocumentView.SelectionManager.SelectionLength();

In Visual Basic:

Dim lengthOfSelection As Integer
lengthOfSelection = Me.xamSyntaxEditor1.ActiveDocumentView.SelectionManager.SelectionLength()

Related Topics

The following topics provide additional information related to this topic.

Topic Purpose

This topic lists the events and supported commands available to you, while building your xamSyntaxEditor applications.

This topic will help you understand the document splitting capability of the xamSyntaxEditor and how to customize it.