windowsforms-118972-controls-and-libraries-rich-text-editor-examples-layout-how-to-count-the-lines-in-the-document.md
To count the lines in the document (document rows), create an instance of the LayoutVisitor descendant and let it traverse the page layout. When it encounters a new row, it calls the VisitRow method. This method increments the row counter to count the lines on a page.
To run the visitor each time after building the document layout, handle the DocumentLayout.DocumentFormatted event.
private void DocumentLayout_DocumentFormatted(object sender, EventArgs e) {
this.BeginInvoke((MethodInvoker)(() =>
{
if (this.Visible) {
MyLayoutVisitor visitor = new MyLayoutVisitor(richEditControl1.Document);
int pageCount = richEditControl1.DocumentLayout.GetFormattedPageCount();
for (int i = 0; i < pageCount; i++) {
visitor.Visit(richEditControl1.DocumentLayout.GetPage(i));
}
resultBarStaticItem.Caption = String.Format("Document has {0} lines", visitor.RowIndex);
}
}));
}
Private Sub DocumentLayout_DocumentFormatted(ByVal sender As Object, ByVal e As EventArgs)
Me.BeginInvoke(CType(Sub()
If Me.Visible Then
Dim visitor As New MyLayoutVisitor(richEditControl1.Document)
Dim pageCount As Integer = richEditControl1.DocumentLayout.GetFormattedPageCount()
For i As Integer = 0 To pageCount - 1
visitor.Visit(richEditControl1.DocumentLayout.GetPage(i))
Next i
resultBarStaticItem.Caption = String.Format("Document has {0} lines", visitor.RowIndex)
End If
End Sub, MethodInvoker))
End Sub