Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only NEW text from TextBox?

Tags:

c#

wpf

textbox

xaml

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?

like image 485
PiZzL3 Avatar asked Nov 27 '25 09:11

PiZzL3


2 Answers

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
}
like image 114
Reddog Avatar answered Nov 30 '25 00:11

Reddog


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;
    }
}
like image 35
Mario Avatar answered Nov 30 '25 00:11

Mario