0

I'm using a richtextbox as a VT100 logging terminal. I want to add a search box to the application , with richtextbox.Find(). The problem is that I want to highlight the first found string without using SelectionColor, as it will change the previous color. And I want to avoid to loose it. If I'm using the mouse to select the text, it's highlighted and keeping the color. I don't know if it's possible to emulate that by code. Or maybe there's a simpler solution :)

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
SadE
  • 1
  • 1
  • " If I'm using the mouse to select the text" actually these are two different thigs. Changing colour is one and selection is a different thing. You would need to use the `SelectionStart` property or the `Select` method like described in [this post](https://stackoverflow.com/questions/22505079/select-text-from-a-richtextbox-in-c-sharp). Apparently you can even choose the selection colour by setting the `SelectionBackColor` property – Mong Zhu Sep 18 '20 at 11:06
  • Nobody is forcing you to use the SelectionColor property, you can just `richTextBox1.Select(10, 10);` to select `10` chars from position `10`. If you click elsewhere, the selection is removed. So, you can use the position that `.Find()` returns, store the position, then select from that position to the length of the string you found (if it was found, that is). – Jimi Sep 18 '20 at 11:31
  • With Select(), nothing is highlighted. I need to used SelectionBackColor to get the selection visible. But then , I loose the possible previously set color. Except if I keep track of the color for each char before the selection and restore back after unselected... – SadE Sep 18 '20 at 11:39
  • *With Select(), nothing is highlighted*: this is of course not true. If you have the focus elsewhere and `HideSelection = true`, then the selection is clearly *hidden*. Either set the focus on the control before calling those methods or set `HideSelection = false`. There's also the chance that you're using those methods the wrong way, but you didn't post any code.... Probably because you didn't take the [Tour](https://stackoverflow.com/tour), so you missed, among other things, [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Jimi Sep 18 '20 at 12:15

0 Answers0