文本在WPF RichTextBox的自动更换文本、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 t...

我有一个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 :)

阅读全文

相关推荐

最新文章