Windows Develop Bookmark and Share   
 index > Windows Forms General > Strange RichTextBox.Select(0,x) Problem / Bug?
 

Strange RichTextBox.Select(0,x) Problem / Bug?

I have a databound form, one of the fields is a note field and I have it displayed in a RichTextBox. I want to be able to highlight certain text in the richtextbox.

If I use something like:

RTB.Select(5, 5)
RTB.selectionBackColor = Color.Yellow

Everything works as expected, the 6th - llth characters are highlighted, when I move to the next record nothing is highlighted as expected.

The strange problem I have come across is if I use a start index of 0

RTB.Select(0, 5)
RTB.selectionBackColor = Color.Yellow

When I do this the first 5 characters are highlighted as expected but if I move to the next record or any other record from that point forward all the text is highlighted. The highlighting only goes away if I move to a blank note field.

Has anyone else come across this problem before? Any suggestions?

I have tried all methods of selecting the text and I get the same result, I even just created a new form with no other code and just the Rtb note field and I get the same result.

Thanks
Kliot  Thursday, August 06, 2009 11:41 PM
Thank you for the suggestions Ihave tried to reset the colorasreccomendedbutit doesn't solve the problem and it doesn't address the question as to why the problem only occurs when the start index is 0. The temporary solution I have come up with is if the start index is 0 I set it to 1 and everythink works like it should but obviously this is not a great solution....
Kliot  Friday, August 07, 2009 2:11 AM
Just put some code in your move to next record routine to clear the selection:

richTextBox1.SelectionLength = 0;

BinaryCoder  Thursday, August 06, 2009 11:44 PM
Do you make a point of clearing the selection and clearing the background color when you change records?
I mean do you actively set the selection to nothing and the selection backcolor to transparent?

tlhintoq  Thursday, August 06, 2009 11:46 PM
You have to restore the SelectionColor property back to the default:

RTB.Select(0, 5)
dim old as Color = RTB.SelectionBackColor
RTB.selectionBackColor = Color.Yellow
RTB.Select(0, 0)
RTB.SelectionBackColor = old


Hans Passant.
nobugz  Friday, August 07, 2009 12:41 AM
Thank you for the suggestions Ihave tried to reset the colorasreccomendedbutit doesn't solve the problem and it doesn't address the question as to why the problem only occurs when the start index is 0. The temporary solution I have come up with is if the start index is 0 I set it to 1 and everythink works like it should but obviously this is not a great solution....
Kliot  Friday, August 07, 2009 2:11 AM

You can use google to search for other answers

Custom Search

More Threads

• How do you disable focus of a control....
• Docking issues VS2k3 driving me mad
• windows forms controls render as red colored Xs
• How does BackgroundWorker use async thread to munipulate UI
• DisplayMember is not displayed
• About virtual memory
• Accessing parent Form from a thread
• Select All/Deselect All Checklistbox
• Parent-Child relationship - Only want parent rows in datagrid
• Identifing correct latest files from the Internet Temp Folder