Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone!
i need to know how to unhighlight text in a Rich Text Box.
heres what i have so far:
C#
//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);

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
Posted
Comments
BillWoodruff 25-Dec-14 10:58am    
Is all the Text in the RichTextBox in one foreground, and one background, color ?
MasterCodeon 25-Dec-14 11:02am    
huh?
Afzaal Ahmad Zeeshan 25-Dec-14 11:32am    
He is talking about the Visual Representation of the text, the formatting the Text.
BillWoodruff 25-Dec-14 11:34am    
I am asking you if the RichTextBox Text contains words, characters, or lines, or paragraphs which have different foreground, or background colors. Or, is everything using one color for foreground, another color for background ?
MasterCodeon 25-Dec-14 12:26pm    
oh yeah everything that is not highlighted has a Black forground color and a white background color

1 solution

Getting ready to traverse the Text contents of the RichTextBox word-by-word:
C#
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;
    }
}
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 ?
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900