How to print line numbers for a text field in C #

This will be a long post. I would like to have suggestions, if any, in my procedure. I want a better way to print line numbers next to each CRLF-terminated-line in a richtextbox. I am using C # with .NET. I tried using ListView, but it is inefficient when the number of rows grows. I managed to use the graphics in the user control to print line numbers, and so far I am satisfied with the performance.

But as the number of rows increases from 50K to 100K, scrolling is greatly degraded. I overridden the WndProc method and processed all messages to cause line numbers to be printed only when needed. (Overriding OnContentsResized and OnVScroll makes redundant calls to the print method.)

Now printing the line number is fine when the number of lines is small, say, up to 10 KB (which I am fine with, since I rarely need to edit a file with 10,000 lines), but I want to remove the restriction.

Few observations

  • The number of lines displayed in richtexbox is equal to the constant + -1. Thus, the difference in performance should be due to the large text, and not because I use graphics.
  • Drawing line numbers for large text are slower compared to small files

Now pseudo code

FIRST_LINE_NUMBER = _textBox.GetFirstVisibleLineNumber(); LAST_LINE_NUMBER = _textBox.GetLastVisibleLineNUmber(); for(loop_from_first_to_last_line_number) { Y = _textBox.GetYPositionOfLineNumber(current_line_number); graphics_paint_line_number(current_line_number, Y); } 

I use GetCharIndexFromPosition and go through RichTextBox.Lines to find the line number in both functions that get line numbers. To get the Y position, I use GetPositionFromCharIndex to get the Point structure.

All of the above RichTextBox methods seem to be O (n), which enliven performance. (Correct me if I am wrong.)

I decided to use a binary tree to store line numbers in order to improve the search when searching for a line number using char index. I have an idea to get a data structure that takes the time to build O (n), O (nlgn) worst case and search O (lgn).

Is this approach worth the effort? Is there any other approach to solving the problem? If necessary, I am ready to write a control from scratch, I just want it to be easy and fast.

+4
source share
1 answer

Before embarking on a better path forward, we need to make sure that we understand the bottleneck.

First of all, it’s important to know how RichTextbox (which I assume you use as you mentioned) handles large files. Therefore, I would recommend deleting all line printing and see how it works with large text. If he is bad, there is your problem.

The second step is to supply some profiling operators or simply use the profiler (one comes with VS 2010) to find the bottleneck. This may turn out to be a method of finding the line number or something else.

At this point, I would suggest only more investigations . If you have completed the investigation and received more information, please update your question and I will definitely return to you.

+2
source

All Articles