文本在WPF RichTextBox的自动更换 [英] automatic replacement of text in wpf richtextbox

查看:196
本文介绍了文本在WPF RichTextBox的自动更换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF .NET 4的C#的RichTextBox ,我想在与其他字符的文本框替换某些字符,这是发生在的KeyUp 事件。

I have a WPF .NET 4 C# RichTextBox and I'm wanting to replace certain characters within that text box with other characters, this is to happen on the KeyUp event.

我试图做到的,是全词,例如替换首字母缩写词:
PC =个人电脑
SC =星际争霸
等等...

What I'm trying to achieve is replace acronyms with full words, e.g.:
pc = personal computer
sc = starcraft
etc...

我已经看了一些类似的主题,但任何事情我发现一直没有成功我的方案。

I've looked a few similar threads, but anything I've found hasn't been successful in my scenario.

最后,我希望能够与缩略语表做到这一点。不过,我在使用,甚至取代单一的首字母缩写的问题,任何人都可以帮忙吗?

Ultimately, I'd like to be able to do this with a list of acronyms. However, I'm having issues with even replacing a single acronym, can anyone help?

推荐答案

由于 System.Windows.Controls.RichTextBox 不具有属性文字来检测它的价值,你可能会使用其检测值以下

Because System.Windows.Controls.RichTextBox does not have a property for Text to detect its value, you may detect its value using the following

string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;

然后,您可以更改 _text ,并使用以下发布新的字符串

Then, you may change _Text and post the new string using the following

_Text = _Text.Replace("pc", "Personal Computer");
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
}

所以,它看起来会像这样

So, it'd look like this

string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
_Text = _Text.Replace("pc", "Personal Computer"); // Replace pc with Personal Computer
if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
{
new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text; // Change the current text to _Text
}

备注:除了使用 Text.Replace(下称电脑,个人电脑); ,你可以声明一个列表与LT;字符串> 中保存字符,而其替代品

Remark: Instead of using Text.Replace("pc", "Personal Computer"); you may declare a List<String> in which you save the characters and its replacements

示例:

    List<string> _List = new List<string>();
    private void richTextBox1_TextChanged(object sender, TextChangedEventArgs e)
    {

        string _Text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text;
        for (int count = 0; count < _List.Count; count++)
        {
            string[] _Split = _List[count].Split(','); //Separate each string in _List[count] based on its index
            _Text = _Text.Replace(_Split[0], _Split[1]); //Replace the first index with the second index
        }
        if (_Text != new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text)
        {
        new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd).Text = _Text;
        }
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        // The comma will be used to separate multiple items
        _List.Add("pc,Personal Computer");
        _List.Add("sc,Star Craft");

    }

谢谢,
我希望对您有所帮助:)

Thanks,
I hope you find this helpful :)

这篇关于文本在WPF RichTextBox的自动更换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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