2016-05-20 6 views
-1

WPFにRichTextBox(Name="txtGoTo")があり、最初の文字は赤で塗りつぶすだけです(残りの部分は黒です)。RichTextBox:最初の文字の色が異なります。

これは私が試した(オンラインそれを得た)ものですが、それは動作しません:

Dim rtb As RichTextBox = txtGoTo 
    Dim Text As String = New TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text 

    If Not String.IsNullOrEmpty(Text) Then 

     Dim RangeAisle As TextPointer = rtb.Document.ContentStart 
     Dim RangeShelf As TextPointer = rtb.Document.ContentStart.GetPositionAtOffset(1) 

     Dim tr As New TextRange(RangeAisle, RangeShelf) 
     tr.ApplyPropertyValue(RichTextBox.ForegroundProperty, Brushes.Red) 
    End If 

答えて

0

あなたの問題はrtb.Document.ContentStart.GetPositionAtOffset(1)は、最初の文字を返さないということです。 3

以下のコードは、最初の文字の位置を取得して赤に変わります。

Dim position As TextPointer = rtb.Document.ContentStart 
While position IsNot Nothing 
    If position.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then 
     Dim start As TextPointer = position.GetPositionAtOffset(0, LogicalDirection.Forward) 
     Dim [end] As TextPointer = position.GetPositionAtOffset(1, LogicalDirection.Backward) 
     New TextRange(start, [end]).ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red) 
     Exit While 
    End If 
    position = position.GetNextContextPosition(LogicalDirection.Forward) 
End While 
+0

Visual Studioは、私に新しいTextRangeの 'の新しい下の構文エラー(起動、[終了])。ApplyPropertyValue(TextElement.ForegroundProperty、Brushes.Redを)'与えます –

関連する問題