Question

I am inserting lists into a RichTextBox like this - but how do I get the Caret to move to the first list item?

 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
Était-ce utile?

La solution 2

Turns out the answer was a lot simpler that I thought :-)

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

Complete

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

Autres conseils

The easy part is setting the position of the caret... the tricky part is finding the pointer of the place that you want to set it to (unless that is simply the beginning or end of the document):

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;

From RichTextBox.CaretPosition Property on MSDN.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top