Search code examples
silverlightrichtextboxlineline-count

Get line count from Silverlight RichTextBox?


Does anyone of a way to get a line count from a Silverlight RichTextBox? I've tried counting the <Run> tags but that doesn't seem to be very accurate. The text in the RichTextBox will be different each time, so I need a good solid way to calculate how many lines of text are when the user is finished typing.

Does anyone know of a way to do this?


Solution

  • Well I had this code involving the run tags too (I didn't write it and I can't remember where it was taken from so I won't take any credit for it). Is it different from yours?

    int blockCount = 0; 
    int lineCount = 0; 
    foreach (Block b in myRTB.Blocks) 
    { 
        if (b is Paragraph) 
        { 
            p = new Paragraph(); 
            p = b as Paragraph; 
            foreach (Run run in p.Inlines) 
            { 
                lineCount++; 
            } 
            blockCount++; 
        } 
    }