Reputation: 2210
private void tbLog_TextChanged(object sender, TextChangedEventArgs e)
{
//Get only NEW text added to Log
}
/*
LOG
old message...
old message...
old message...
old message...
NEW message...
NEW message...
NEW message...
NEW message...
NEW message...
*/
How to I get only NEW text from the TextBox?
Upvotes: 2
Views: 10143
Reputation: 321
private void textTx1Asc_TextChanged(object sender, EventArgs e)
{
string s;
//get only the new chars
s = textTx1Asc.Text;
s = s.Remove(0, prev_len);
//update prev_len for next time
prev_len = textTx1Asc.TextLength;
//s contains only the new characters, process here
}
Upvotes: 0
Reputation: 15579
Perhaps you should be using the TextChangedEventArgs.Changes
property:
var fullText = tbLog.Text;
if (e.Changes.Any())
{
var additions = e.Changes.Where(tc => tc.AddedLength > 0);
var newTexts = additions.Select(tc => fullText.Substring(tc.Offset, tc.AddedLength));
// TODO: Do stuff with the new pieces of text
}
Upvotes: 6
Reputation: 10062
For desktop WPF text boxes, you should be able to use TextChangedEventArgs.Changes to enumerate the changes. Note a single event may contain several changes.
The TextChange class has Offset, AddedLength, and RemovedLength properties that give you the exact range of characters that were changed.
Upvotes: 3
Reputation: 36487
Something like this?
private string old_text = "";
private void tbLog_TextChanged(object sender, TextChangedEventArgs e)
{
if(old_text != tbLog.Text)
{
writeLog(tbLog.Text);
old_text = tbLog.Text;
}
}
Upvotes: 3