Problems with TextDocumentChangingEventArgs

SyntaxEditor for WPF Forum

Posted 11 years ago by Richard Hertzberg
Platform: .NET 3.5
Environment: Windows Vista (32-bit)
Avatar
I'm using the EditorDocument.TextChanging event to support a MaxLength property similar to that of a WPF TextBox. The idea is to cancel the update if the modification causes the text length to exceed a defined value. The TextDocumentChangingEventArgs datum that my event handler receives doesn't appear to hold data appropriate to the event. I don't appear to be able to get a specification of the new text. Specifically:
1. The NewSnapshot item is always null.
2. The Snapshot member of the TextChange item appears to supply the current text, not the new text.

Following is the code that I'm using:

EditorDocument EDoc = new EditorDocument ();
EDoc.TextChanging += OnDocumentTextChanging;
...
private void OnDocumentTextChanging (object Sender,TextDocumentChangingEventArgs Evt)
{
ITextSnapshot NSnap = Evt.NewSnapshot;
ITextSnapshot OSnap = Evt.OldSnapshot;

if (NSnap == null) // [[why is this null]]?
{
ITextChange TUpd = Evt.TextChange; // try using TextChange property
NSnap = (TUpd == null ? null : TUpd.Snapshot);
}
// get new and old text lengths
int nLengthN = (NSnap == null ? 0 : NSnap.Text.Length);
int nLengthO = (OSnap == null ? 0 : OSnap.Text.Length);

if (nLengthN == nLengthO) // [[if text was added, these should differ]]
nLengthN = nLengthO; // [[why is new snapshot giving old length?]]

if (m_nMaxLength > 0 && nLengthN > m_nMaxLength)
Evt.Cancel = (nLengthN >= nLengthO); // allow a length reduction
}

Thanks for your help. If there's any difficulty reproducing this, let me know and I'll send you my test project.

Comments (1)

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Richard,

Thanks for the bug report, we found the problem and have it fixed for the next build. The NewSnapshot should have been filling in properly.

The ITextChange.Snapshot will still point to the original snapshot upon which it is based though. That is intended.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.