如何打印行号的文本框,在C# [英] How to print line numbers for textbox in c#

查看:204
本文介绍了如何打印行号的文本框,在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这将是一个很长的帖子。我想有如有的过程,我提出以下建议。我想最好的方法来打印一个接一个CRLF终止行的一个RichTextBox行号。我使用C#在.NET。我已经使用ListView的尝试,但它是低效的,当行数增长。我在使用图形自定义控制打印的行数,到目前为止我很高兴的表现是成功的。

This is going to be a long post. I would like to have suggestions if any on the procedure I am following. I want the best method to print line numbers next to each CRLF-terminated-line in a richtextbox. I am using C# with .NET. I have tried using ListView but it is inefficient when number of lines grow. I have been successful in using Graphics in custom control to print the line numbers and so far I am happy with the performance.

但是作为行数增长到50K到100K的滚动受到严重影响。我已重写WndProc方法和处理所有的消息调用行号仅在需要时打印。 (覆盖OnContentsResized和OnVScroll使多余调用的打印方式)。

But as the number of lines grow to 50K to 100K the scrolling is affected badly. I have overridden WndProc method and handling all the messages to call the line-number printing only when required. (Overriding OnContentsResized and OnVScroll make redundant calls to the printing method).

现在的线号印字是正常时的行数较少的发言权高达10K(与我很好,因为它是罕见的需要编辑有10000行的文件),但我想删除的限制。

Now the line number printing is fine when number of lines is small say upto 10K (with which I am fine as it is rare need to edit a file with 10000 lines) but I want to remove the limitation.

小样本

  • 在richtexbox显示的行数是恒定的+ -1。因此,性能差异应该是由于大量的文字,而不是因为我使用的图形画。
  • 对于大型文本
  • 在画行号时相比,小文件速度慢

现在的伪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);
}

我使用的 GetCharIndexFromPosition 的环通过RichTextBox.Lines找到这两者获得的行号的函数的行号。为了让我使用Y位置的 GetPositionFromCharIndex 的得到的的结构。

I am using GetCharIndexFromPosition and loop through the RichTextBox.Lines to find the line number in both the functions which get the line numbers. To get Y position I am using GetPositionFromCharIndex to get the Point struct.

以上所有的RichTextBox的方法似乎是为O(n),它吃掉的表现。 (纠正我,如果我错了。)

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

我已经决定使用二叉树来存储行号由字符的索引搜索的行号的时候,以提高搜索性能比较。我得到一个数据结构,需要O(n)的施工时间的想法,O(nlgn)最坏情况下的更新和O(LGN)的搜索。

I have decided to use a binary-tree to store the line numbers to improve the search perfomance when searching for line number by char index. I have an idea of getting a data-structure which takes O(n) construction time, O(nlgn) worst-case-update, and O(lgn) search.

时的这种做法值得的? 是否有任何其他的方法来解决这个问题?如果需要,我愿意从头开始编写的控制,我只是希望它重量轻,速度快。

Is this approach worth the effort? Is there any other approach to solve the problem? If required I am ready to write the control from scratch, I just want it to be light-weight and fast.

推荐答案

在决定未来的最好方法,我们需要确保我们理解的瓶颈。

Before deciding on the best way forward, we need to make sure we understand the bottleneck.

首先,重要的是要知道的RichTextbox(我假设你正在使用你提到它)如何处理大型文件。因此,我建议删除所有行打印的东西,看看它有大量的文字进行。如果是差,有你的问题。

First of all, it is important to know how RichTextbox (which I assume you are using as you mentioned it) handles the large files. So I would recommend to remove all line printing stuff and see how it performs with large text. If it is poor, there is your problem.

第二步是把一些分析性陈述或者只是使用一个分析器(一个自带的VS 2010)发现的瓶颈。它可能会变成是方法查找的行号,或别的东西。

Second step would be to put some profiling statements or just use a profiler (one comes with the VS 2010) to find the bottleneck. It might turn out to be the method for finding the line number, or something else.

在这一点上,我只会建议的更多的调查。如果你已经完成了调查,并有更多的信息,更新你的问题,我会尽快给你相应。

At this point, I would only suggest more investigation. If you have finished the investigation and have more info, update your question and I will get back to you accordingly.

这篇关于如何打印行号的文本框,在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆