计算由RichTextBox中显示的行用C#RichTextBox

2023-09-03 20:33:21 作者:心已被拧碎

在我的项目,我想指望通过的richtextbox在C#中有设置为true自动换行属性显示的行。例如,在RichTextBox中,我写的文本,并且当文本到达的richtextbox的宽度,那么它会自动进入第二行。所以会发生什么是文本实际上包含一条线,但它显示在两行。所以,我想知道我怎么能指望这为两行,而不是吗?帮助将非常AP preciated。

In my project I want to count lines displayed by richtextbox in C# with word wrap property set true. For example, in richtextbox, I write a text, and when text reaches the width of the richtextbox, then it automatically goes to second line. So what happens is the text contains actually one line but it is displayed in two lines. So I want to know how can I count this as two lines instead of one? Help will be really appreciated.

推荐答案

您可以去RichTextBox.GetLineFromCharIndex方法

检索从行号   在指定的字符位置   RichTextBox控件的文本。如果自动换行设置为,该行的任何部分换到下一个,该方法的返回0指定的字符索引。

Retrieves the line number from the specified character position within the text of the RichTextBox control. If WordWrap is set to false, no portion of the line wraps to the next, and the method returns 0 for the specified character index.

此外,检查出这个链接。