我如何查找已使用TextChanged添加了哪些文本 [英] How do I find what text had been added with TextChanged

查看:102
本文介绍了我如何查找已使用TextChanged添加了哪些文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在文本框中的文本和变量中的字符串之间进行同步的方法.我找到了如何获取更改字符串的索引(在文本框中),添加的长度和删除的长度,但是实际上如何找到添加的字符串?

I'm looking to synchronize between a text in the textbox and string in a variable. I found how to get the index in which the string was changed (in the textbox), the length added and length removed, but how can I actually find the string added?

到目前为止,我已经使用了TextChangedEventArgs.Changes,并获得了其中的项目的属性(ICollection).

So far I've used TextChangedEventArgs.Changes, and got the properties of the items in it (ICollection).

我正在尝试创建一个密码框,在其中可以通过功能显示实际密码.因此,我不希望文本框直接进行同步(例如,在文本框中会出现"*****",在字符串中出现"hello").

I'm trying to create a password box in which I could show the actual password by a function. hence I do not want the textbox to synchronize directly (for example, in the textbox would appear "*****" and in the string "hello").

推荐答案

如果只想添加文本,则可以这样做

If you want only text added you can do this

 string AddedText;
 private void textbox_TextChanged(object sender, TextChangedEventArgs e)
 {
     var changes = e.Changes.Last();
     if (changes.AddedLength > 0)
     {
         AddedText = textbox.Text.Substring(changes.Offset,changes.AddedLength);
     }
 }

修改

如果您想全部添加和删除文本,可以执行此操作

If you want all added and remove text you can do this

    string oldText;
    private void textbox_GotFocus(object sender, RoutedEventArgs e)
    {
        oldText = textbox.Text;
    }

    string AddedText;
    string RemovedText;
    private void textbox_TextChanged(object sender, TextChangedEventArgs e)
    {
        var changes = e.Changes.Last();
        if (changes.AddedLength > 0)
        {
            AddedText = textbox.Text.Substring(changes.Offset, changes.AddedLength);
            if (changes.RemovedLength == 0)
            {
                oldText = textbox.Text;
                RemovedText = "";
            }
        }
        if (changes.RemovedLength > 0)
        {
            RemovedText = oldText.Substring(changes.Offset, changes.RemovedLength);
            oldText = textbox.Text;
            if (changes.AddedLength == 0)
            {
                AddedText = "";
            }
        }
    }

这篇关于我如何查找已使用TextChanged添加了哪些文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆