void _textDocument_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "CurrentSnapshot")
{
//TODO: Do logic as required when a new snapshot is created.
}
}
This topic introduces the key TextDocument
class events and includes code snippets to illustrate their uses.
This topic contains the following sections:
The PropertyChanged event is raised whenever the value of a TextDocument’s
property changes.
When handling this event, the TextDocument
object will be sent to the event handler as “sender,” and the PropertyChangedEventArgs object “e” will contain the name of the property that has changed, as e.PropertyName.
The following code example shows the event handler setup for executing logic when the TextDocument’s
CurrentSnapshot changes.
In C#:
void _textDocument_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "CurrentSnapshot")
{
//TODO: Do logic as required when a new snapshot is created.
}
}
In Visual Basic:
Private Sub _textDocument_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)
'TODO: Do logic as required when a new snapshot is created.
If e.PropertyName = "CurrentSnapshot" Then
End If
End Sub
Changes applied to the document raise the TextChanged event. It exposes the before and after snapshots as well as the list of all the changes.
The following code example is handling the TextChanged event and looping through all of the changes made in the document.
In C#:
void _textDocument_TextChanged(object sender, TextChangedEventArgs e)
{
foreach (TextChange tc in e.Changes)
{
//TODO: Loop through the text changes to perform logic based on the changes
//that have been made.
}
}
In Visual Basic:
Private Sub _textDocument_TextChanged(sender As Object, e As TextChangedEventArgs)
For Each tc As TextChange In e.Changes
'TODO: Loop through the text changes to perform logic based on the
'changes that have been made.
Next
End Sub
Raised before document changes are applied, the TextChanging event may be triggered by any methods that update text within a document, such as Delete, Append, FindReplace, etc. This event is cancellable if its CanCancel property returns true (which it does, unless the source of the change is an undo or redo operation). It exposes the before and after snapshots as well as the list of changes.
The following code example is handling the TextChanging event and checking if the document is growing from this change. It is also cancelling the event in a scenario “someCondition”, specified by the programmer.
In C#:
void _textDocument_TextChanging(object sender, TextChangingEventArgs e)
{
if (e.After.Document.CurrentSnapshot.Length > e.Before.Document.CurrentSnapshot.Length)
{
//TODO: Do some logic when the document grows.
//TODO: Under some circumstance, cancel the event.
if (someCondition == true && e.CanCancel == true)
e.Cancel = true;
}
}
In Visual Basic:
Private Sub _textDocument_TextChanging(sender As Object, e As TextChangingEventArgs)
If e.After.Document.CurrentSnapshot.Length > e.Before.Document.CurrentSnapshot.Length Then
'TODO: Do some logic when the document grows.
'TODO: Under some circumstance, cancel the event.
If someCondition = True AndAlso e.CanCancel = True Then
e.Cancel = True
End If
End If
End Sub
Calling the Load() or InitializeText() method raises the TextLoaded event. This event is also raised whenever the Language changes on a TextDocument containing text.
The following topic provides additional information related to this topic.