Results 1 to 7 of 7

Thread: A little over my head... detecting/counting by font color in richtextbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    21

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    21

    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!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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:
    1. Dim blackLineCount = 0
    2. Dim selectionStart = 0
    3.  
    4. For Each line As String In RichTextBox1.Lines
    5.     Dim lineLength = line.Length
    6.  
    7.     With RichTextBox1
    8.         .SelectionStart = selectionStart
    9.         .SelectionLength = lineLength
    10.  
    11.         If .SelectionColor = Color.Black Then
    12.             blackLineCount += 1
    13.         End If
    14.     End With
    15.  
    16.     selectionStart += lineLength + 1
    17. 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.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    21

    Re: A little over my head... detecting/counting by font color in richtextbox

    Perfection! Thank you!

    All the best... SMS

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2012
    Posts
    21

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width