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.
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.