0

我将列表插入到这样的 RichTextBox 中 - 但是如何让插入符号移动到第一个列表项?

 Private Sub TextEditor_BulletListAdd(sender As Object, e As RoutedEventArgs)
    Try
      Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim vList As New List()
        vList.MarkerStyle = TextMarkerStyle.Disc
        Dim vRun As New Run()
        Dim vItem As New ListItem(New Paragraph(vRun))
        vList.ListItems.Add(vItem)
        Dim curCaret = vEditor.CaretPosition
        Dim curBlock = vEditor.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
        vEditor.Document.Blocks.InsertAfter(curBlock, vList)

    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

Private Sub TextEditor_NumberListAdd(sender As Object, e As RoutedEventArgs)
    Try
        Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim vList As New List()
        vList.MarkerStyle = TextMarkerStyle.Decimal
        Dim vRun As New Run()
        Dim vItem As New ListItem(New Paragraph(vRun))
        vList.ListItems.Add(vItem)
        Dim curCaret = vEditor.CaretPosition
        Dim curBlock = vEditor.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
        vEditor.Document.Blocks.InsertAfter(curBlock, vList)
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub
4

2 回答 2

1

简单的部分是设置插入符号的位置......棘手的部分是找到要设置的位置的指针(除非这只是文档的开头或结尾):

RichTextBox rtb = new RichTextBox(flowDoc);
// Get the current caret position.
TextPointer caretPos = rtb.CaretPosition;
// Set the TextPointer to the end of the current document.

caretPos = caretPos.DocumentEnd; // <<< You need to find the correct position here

// Specify the new caret position at the end of the current document.
rtb.CaretPosition = caretPos;

来自MSDN 上的RichTextBox.CaretPosition 属性

于 2013-07-23T11:41:33.873 回答
1

原来答案比我想的要简单得多:-)

 Dim vMove As TextPointer = curCaret.GetNextInsertionPosition(LogicalDirection.Forward)
        If Not vMove Is Nothing Then
            vEditor.CaretPosition = vMove
        End If

完全的

Private Sub TextEditor_BulletListAdd(sender As Object, e As RoutedEventArgs)
    Try
      Dim vEditor As RichTextBox = TextEditorGrid.FindName("Controls_TextEditorRTF")
        Dim vList As New List()
        vList.MarkerStyle = TextMarkerStyle.Disc
        Dim vRun As New Run()
        Dim vItem As New ListItem(New Paragraph(vRun))
        vList.ListItems.Add(vItem)
        Dim curCaret = vEditor.CaretPosition
        Dim curBlock = vEditor.Document.Blocks.Where(Function(x) x.ContentStart.CompareTo(curCaret) = -1 AndAlso x.ContentEnd.CompareTo(curCaret) = 1).FirstOrDefault()
        vEditor.Document.Blocks.InsertAfter(curBlock, vList)
        Dim vMove As TextPointer = curCaret.GetNextInsertionPosition(LogicalDirection.Forward)
        If Not vMove Is Nothing Then
            vEditor.CaretPosition = vMove
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub
于 2013-07-23T13:43:51.073 回答