的RichTextbox SelectionStart返回错误的索引索引、错误、RichTextbox、SelectionStart

2023-09-06 05:09:00 作者:你的眼眸印在我心间

我需要向用户显示,像NOTEPAD.EXE,选择开始和他对光标文字的长度。

选择长度是没有问题的,因为RichTextBox的支持选择属性开始和结束。

http://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer.getoffsettoposition.aspx

但我RichTexbox的startIndex是永远 2 而不是 0 ,如果我设置的第一个位置的光标该文档。 如果我清楚完整的文字是在 0 。但是,如果我preSS SPACE 然后退格的文本框为空,但开始索引的计数器上的 2

你知道吗?

*编辑* 第一个解决方案

好了,这就是我的工作液。但是,我认为这是一个更好的办法来做到这一点。

 '''<总结>
    '''获取光标的位置。忽略所有格式字符,如回车和段落。只是计数可见字符。
    '''< /总结>
    '''< PARAM NAME =RTB>将RichTextBox的值应确定和LT; /参数>
    '''<返回>将光标的指标值。 0是在所述第一位置。后位置的背后是字符123,它会返回该指数3< /回报>
    '''<说明>提防的表现,分开使用此梅索德。蒂莫·鲍姆(Böhme),2012< /说明>
    专用功能GetPositionOfCursor(BYVAL RTB作为RichTextBox中)作为整数
        昏暗contentStart作为TextPointer = rtb.Document.ContentStart
        昏暗的资源为整数= 0
        昏暗CursorIndex作为整数= contentStart.GetOffsetToPosition(rtb.CaretPosition)
        昏暗Ĵ作为整数= 0

        做
            如果J> CursorIndex然后退出待办事项
            如果contentStart.GetPositionAtOffset(1,LogicalDirection.Forward)是没有那么
                退出待办事项
            elseif的contentStart.GetPointerContext(LogicalDirection.Backward)= TextPointerContext.Text然后
                RES + = 1
            结束如果

            contentStart = contentStart.GetPositionAtOffset(1,LogicalDirection.Forward)
            J + 1 =
        循环

        返回水库
    端功能
 

解决方案

我不知道这是否是一个真正的回答你的问题,但我用这个简单的技巧来获取TOT文本相关的光标指数:

的TextRange范围=新的TextRange(Document.ContentStart,CaretPosition); INT N = range.Text.Length;

我工作的基础上,WPF的RichTextBox的编辑器。由于实时的格式(如突出的关键字等等)实在是太慢了我创建另一个线程一个新的文档。在这个线程的文字被格式化为appropiate运行,而不是将它们作为格式在RichTextBox的段落中的一部分。一旦完成了原来的被替换为新的。作品真的很好,令人难以置信的速度快(与MS的方法至少)。

我希望这能给你一些启发和/或想法。

I need to show the user, like in notepad.exe, the selection start and length of his text on cursor.

c winform richTextBox1.LoadFile加载MemoryStream的时候报错

Selection length is no problem because Richtextbox supports the Selection Property with Start and End.

http://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer.getoffsettoposition.aspx

But the startindex of my RichTexbox is always 2 instead of 0 if I set the cursor on first position of the document. If I CLEAR the complete text it is on 0. But if I press SPACE and then BACKSPACE the textbox is empty but the counter of StartIndex is on 2

Any idea?

* EDIT * FIRST SOLUTION

Ok, thats a working solution of mine. But I think there is a better way to do it.

''' <summary>
    ''' Get the position of the cursor. Ignores all formatting characters like ENTER and PARAGRAPH. Just counts the visible characters.
    ''' </summary>
    ''' <param name="rtb">The richtextbox the value should be determined</param>
    ''' <returns>Index value of the cursor. 0 is at the first position. After position is behind characters "123" it would return the index 3.</returns>
    ''' <remarks>Watch out for performance, Use this methode in separated. Timo Böhme, 2012</remarks>
    Private Function GetPositionOfCursor(ByVal rtb As RichTextBox) As Integer
        Dim contentStart As TextPointer = rtb.Document.ContentStart
        Dim res As Integer = 0
        Dim CursorIndex As Integer = contentStart.GetOffsetToPosition(rtb.CaretPosition)
        Dim j As Integer = 0

        Do
            If j > CursorIndex Then Exit Do
            If contentStart.GetPositionAtOffset(1, LogicalDirection.Forward) Is Nothing Then
                Exit Do
            ElseIf contentStart.GetPointerContext(LogicalDirection.Backward) = TextPointerContext.Text Then
                res += 1
            End If

            contentStart = contentStart.GetPositionAtOffset(1, LogicalDirection.Forward)
            j += 1
        Loop

        Return res
    End Function

解决方案

I do not know whether this is a real answer to your question, but I use this simple trick to retrieve the cursor index related tot the text:

TextRange range = new TextRange(Document.ContentStart, CaretPosition); int n = range.Text.Length;

I'm working on an editor based on the WPF richtextbox. Since real time formatting (like highlighting keywords and such) is really slow I create a new document in another thread. In this thread the text gets formatted in appropiate runs, rather than formatting them as a part of the richtextbox's paragraph. Once finished the original one is replaced by the new one. Works really nice and incredible fast (compared to the MS way at least).

I hope this give you some inspiration and/or ideas.