简体   繁体   English

自动替换WPF RichTextBox中的文本

[英]automatic replacement of text in wpf richtextbox

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. 我有一个WPF .NET 4 C# RichTextBox并且我想用其他字符替换该文本框中的某些字符,这是在KeyUp事件上发生的。

What I'm trying to achieve is replace acronyms with full words, eg: 我想要实现的是用完整的单词替换首字母缩写词,例如:
pc = personal computer pc =个人电脑
sc = starcraft sc =星际争霸
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? 但是,即使替换一个首字母缩写词我也遇到问题,有人可以帮忙吗?

Because System.Windows.Controls.RichTextBox does not have a property for Text to detect its value, you may detect its value using the following 由于System.Windows.Controls.RichTextBox没有Text可以检测其值的属性,因此您可以使用以下命令检测其值

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

Then, you may change _Text and post the new string using the following 然后,您可以使用以下命令更改_Text并发布新字符串

_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
}

Remark : Instead of using Text.Replace("pc", "Personal Computer"); 备注 :代替使用Text.Replace("pc", "Personal Computer"); you may declare a List<String> in which you save the characters and its replacements 您可以声明一个List<String>在其中保存字符及其替换

Example: 例:

    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 :) 我希望这个对你有用 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM