首页 > 解决方案 > UWP:如何动态选择文本框中最后输入的单词并更改其突出显示颜色?

问题描述

这是我用来获取最后输入的单词的winform代码

string txt = richTextBox1.Text;
string lastWordspace = txt.Substring(txt.LastIndexOf(" ") + 1);
string lastWord = lastWordspace.Substring(lastWordspace.LastIndexOf("\n") + 1);

richTextBox1.SelectionStart = richTextBox1.Text.LastIndexOf(lastWord);
richTextBox1.SelectionLength = lastWord.Length;
richTextBox1.SelectionBackColor = Color.LightCoral;

我想要 UWP 中的相同功能。如何选择文本框中最后输入的单词并使用 c# 动态更改其突出显示颜色?

标签: c#uwpwindows-10

解决方案


在 UWP 中,对应的RichTextBoxRichEditBox.

你可以试试这个:

TestRichEditBox.TextDocument.GetText(Windows.UI.Text.TextGetOptions.None, out string txt);
int lastIndex = txt.Length - 2;
TestRichEditBox.Document.Selection.SetRange(lastIndex, lastIndex + 1);
TestRichEditBox.SelectionHighlightColor = new SolidColorBrush(Colors.LightCoral);

此致。


推荐阅读