简体   繁体   中英

How can I highlight specific text in a richtextbox using C#?

The title might not make sense so here's a more in-depth explanation:

I'm currently working on a UI Typing Test, where the user will be timed while they enter the sample text provided. At the end of the test, it needs to display the time elapsed, words per minute, and I need to "highlight the typing errors in red" where the text was entered.

For example, if the sample was:

"The quick brown fox jumped over the lazy dog." and the user entered:

"The quicc brown fox jumped over the lazy god." the end result would need to look like this :

"The quic c brown fox jumped over the lazy g o d ." (Bold being substituted for highlighted in red)

How would I go about doing this? My searches haven't turned up the results I'm looking for.

Apparently I need to clarify that I am not asking you to write code for me. I'm asking for a nudge in the right direction, as I don't know how to go about extracting a specific part of the text from the box, highlight it and put it back.

You know how to get the RTB.Text vs RTB.Rtf right?

Here is some documentation you might enjoy (or not).

Once you know the difference between those, you get the text, use that to build the rtf string, and then assign that string into the Rtf.

To modify the colors in your rtf string, you should check the documentation on how to format text with rtf. Here is the first google result I found.

Good Luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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