A little over my head... detecting/counting by font color in richtextbox
Hi all...
I have lines in a richtextbox that I wish to count ONLY if they are black in color.
Some lines are light gray, and I don't want to count them.
This is about all I have, but "line" doesn't have properties that relate to font color that I can see.
Code:
For Each line In myRTB.Lines
if line. {some property that returns the font color} = color.black then
theCount = theCount + 1
Next line
I hope this is clear enough...
Thanks for your kind help!
SMS
Re: A little over my head... detecting/counting by font color in richtextbox
You would have to actually select the line in the RichTextBox and then test the SelectionColor property of the control. I'll put together some code but first I want to confirm that the entire line will be one colour in each case.
Re: A little over my head... detecting/counting by font color in richtextbox
Yes, entire line. One color or the other.
One could look at the first character
Thank you!
Re: A little over my head... detecting/counting by font color in richtextbox
This is untested but I believe that this should work:
vb.net Code:
Dim blackLineCount = 0
Dim selectionStart = 0
For Each line As String In RichTextBox1.Lines
Dim lineLength = line.Length
With RichTextBox1
.SelectionStart = selectionStart
.SelectionLength = lineLength
If .SelectionColor = Color.Black Then
blackLineCount += 1
End If
End With
selectionStart += lineLength + 1
Next
Note that the '+ 1' at the end is to account for the line feed that is cut out by splitting the text into lines.
Re: A little over my head... detecting/counting by font color in richtextbox
Perfection! Thank you!
All the best... SMS
Re: A little over my head... detecting/counting by font color in richtextbox
OOPS!
I spoke to soon! This does work great, except that it necessarily scrolls the richtextbox vertically... so that anytime we do this count, the text box jumps out of its current (user set) position to the very bottom.
What might I do to prevent this???
Thank you for you kind help!
SMS
Re: A little over my head... detecting/counting by font color in richtextbox
As you say, the control necessarily scrolls so you will have to scroll it back when you're done. You can remember the original SelectionStart and optionally SelectionLength and set them back when you're done. That will scroll the original selection back into view but that doesn't necessarily mean that it will be in exactly the same position as it was. If that's a problem then you may have to get into some unmanaged code because I don't think that there's a simple managed API to set the exact scrolling position.