C#如何在RichTextBox中取消突出显示文本?

问题描述:

Hello Everyone!

i需要知道如何在富文本框中取消突出显示文字。

继续我到目前为止的内容:

Hello Everyone!
i need to know how to unhighlight text in a Rich Text Box.
heres what i have so far:

//This Selects The Text
TB.Select(ThreadOperation.CharPos,ThreadOperation.TextCurrentlySpeaking.Length);

//This Sets The Select Texts Highlight Color To The System Highlight Color
TB.SelectionColor = Color.White;
TB.SelectionBackColor = Color.FromArgb(255, 51, 153, 255);



现在我想做的就是善良当这行代码运行时,会运行一个语音方法,将当前字符位置输出到属性CharPos,同时它还输出当前所说的单词,因此很复杂。

TextCurrentlySpeaking属性。

现在我已经解释过了,我想要的是文本框只选择语音方法所说的当前单词,但是不要突出显示文本框中的其他内容(或者选择)所以我该怎么做?

感谢您提前的帮助,

MasterCodeon


now what i want to do is kind of complicated.
when this line of code runs there is a speech method running that outputs the current character position to the Property "CharPos", and at the same time its also outputing the word its currently speaking hence the "TextCurrentlySpeaking" property.
now that i have explained that, what i want is for the textbox to only select the current word the speech method is speaking, but make everything else in the textbox not highlighted(or selected) so how would i do this?
thanks for your help in advance,
MasterCodeon

准备逐字遍历RichTextBox的Text内容:
Getting ready to traverse the Text contents of the RichTextBox word-by-word:
private Color HighLightForeColor = Color.Yellow; // your foreground color here
private Color HighLightBackColor = Color.Navy; // your background color here

// the previous Selection Colors
private Color previousSelectionForeColor;
private Color previousSelectionBackColor;

private string theText;

private List<string> theWords = new List<string>();  

private List<point> theWordsLocations = new List<point>();

private char[] splitChar = new[] {' ', '\n'};

private int nWords = 0;

private int currentWord = 0;

private Point previousSelection;

private void Form1_Load(object sender, EventArgs e)
{
    // split the RTB Text
    theWords = richTextBox1.Text.Split(splitChar, StringSplitOptions.RemoveEmptyEntries).ToList();

    // number of words
    nWords = theWords.Count;

    // location pointer
    int location = 0;

    // build the List of word locations
    foreach (string word in theWords)
    {
        theWordsLocations.Add(new Point(location, word.Length));           
        location += word.Length + 1;
    }
}

我们刚建了两个列表,一个用于保存文本中的每个单词,另一个用于保存文本中单词的位置和长度。因此,单词列表中位置#5处的单词将具有单词位置列表中位置#5的点的位置。由于可能存在重复的单词,我们不能在这里使用通用词典。



为方便起见,此处使用Point来保存描述的两个整数一个词的起始位置和长度;一个便宜的方式来保存比元组或其他结构更容易使用的两个整数。



可能的未来问题:



1.文本中的其他类型的空格?标签?



2.现在没有尝试在一个字后立即处理标点字符。



3.这里的字符编码或其他可能的文化因素如何:你能否认为它们总是一样?

We just built two Lists, one to hold each word in the Text, another to hold the location, and length, of the word in the Text. So, the word at position #5 in the list of words will have the location of the Point in position #5 of the list of word locations. Because of the possibility of duplicate words, we can't use a generic Dictionary here.

A Point is used here, for convenience, to hold the two integers that describe a word's start location and length; a "cheap" way to hold two integers that's easier to use than a Tuple, or other structure.

Possible future problems:

1. other kinds of white-space in the Text ? Tabs ?

2. right now no attempt to deal with punctuation characters immediately after a word.

3. what about character encoding, or other possible "culture" factors here: can you assume they're always the same ?