Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select (highlight) text in a .NET TextBox that is set for ReadOnly?

My VB.NET programs displays text to the user that is loaded from a text file. I want to highlight specific parts automatically at various intervals depending on what the application is doing.

I can manually set the highlighted text portion using the TextBox control's SelectionStart and SelectionLength members. Unfortunately, this doesn't work if the TextBox control has its ReadOnly member set to True. What is strange is that my program allows the user to manually click-drag selected text, but text does not get highlighted if the selection is performed programmatically.

The text must be read-only since I do not want the user to modify it. Is there a solution to this?

like image 326
gonzobrains Avatar asked Nov 26 '25 06:11

gonzobrains


1 Answers

You can use the TextBox SelectionStart and SelectionLength properties, but I'm guessing since the TextBox is ReadOnly, it probably doesn't have the focus.

Try setting this property:

TextBox1.HideSelection = False

It should work as you expect to now.

like image 198
LarsTech Avatar answered Nov 28 '25 21:11

LarsTech



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!