Search and highlight text in Silverlight RichTextBox control

Posted by Nakul.Lande under Silverlight category on | Points: 40 | Views : 6165
Silverlight XAML page Code:

<TextBox Name="txtFind" Grid.Column="0" Grid.Row="0" Width="100" Height="20" HorizontalAlignment="Right" SelectionChanged="txtFind_SelectionChanged"></TextBox>
<Button Name="btnFind" HorizontalAlignment="Right" Grid.Column="1" Grid.Row="0" Content="Search" Width="70" Height="20" Click="btnFind_Click" VerticalAlignment="Center"></Button>
<RichTextBox x:Name="rtb" Height="295" VerticalAlignment="Top" BorderBrush="{x:Null}" AcceptsReturn="True" TextInputStart="rtb_TextInputStart" TextWrapping="Wrap" FontSize="12" DataContext="{Binding}" Cursor="Arrow" Foreground="Black" />

Code Behind Page (VB)

''' <summary>
''' This method will search the text in Richtextbox control and highlight it with Red color
''' </summary>
''' <remarks></remarks>
Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)
' First select entire content of text control

Dim runTemp As Run
Dim pointerStart As TextPointer = Nothing
Dim pointerEnd As TextPointer = Nothing
Dim i As Integer = 0
Dim length As Integer = 0
Dim InlineCnt As Integer = 0
Dim TotalBlocksCount As Integer = 0
Dim FindOccurance As Boolean = False
Dim TotalOccurance As Integer = 0
' calculate how many times serach text appear in line
TotalOccurance = UBound(Split(UCase(rtb.Selection.Text.ToUpper.ToString()), UCase(Me.txtFind.Text.Trim.ToUpper.ToString())))

If TotalOccurance <> 0 Then
For Each block As Paragraph In rtb.Blocks
If (TotalBlocksCount = rtb.Blocks.Count And TotalOccurance = 1) Then
TotalBlocksCount = 0
InlineCount = 0
BlockCount = 0
MessageBox.Show("No Further Match Found.")
Exit Do
End If
TotalBlocksCount = TotalBlocksCount + 1
For Each inline In block.Inlines
If (inline.GetType() Is GetType(Run)) Then
runTemp = New Run()
runTemp = DirectCast(inline, Run)
Dim brush1 As New SolidColorBrush(Colors.Black)
rtb.Selection.ApplyPropertyValue(Run.ForegroundProperty, brush1)
If (Not runTemp.Text.ToUpper.Equals(String.Empty)) Then
If runTemp.Text.ToUpper.Contains(Me.txtFind.Text.Trim.ToUpper.ToString()) Then
If InlineCnt = InlineCount Then
If BlockCount = 0 Then
i = InStr(runTemp.Text.ToUpper.ToString(), Me.txtFind.Text.Trim.ToUpper.ToString())
i = InStr(BlockCount, runTemp.Text.ToUpper.ToString(), Me.txtFind.Text.Trim.ToUpper.ToString())
End If
FindCount = i
length = Len(Me.txtFind.Text.ToUpper.ToString())
BlockCount = i + length
InlineCnt = InlineCnt + 1
If i <> 0 Then
pointerStart = runTemp.ElementStart
pointerEnd = runTemp.ElementStart
pointerStart = pointerStart.GetPositionAtOffset(i, LogicalDirection.Forward)
pointerEnd = pointerEnd.GetPositionAtOffset((i + length), LogicalDirection.Backward)
rtb.Selection.Select(pointerStart, pointerEnd)
Dim brush As New SolidColorBrush(Colors.Red)
rtb.Selection.ApplyPropertyValue(Run.ForegroundProperty, brush)
FindOccurance = True
Exit Do
BlockCount = 1
InlineCount = InlineCount + 1
End If
InlineCnt = InlineCnt + 1
End If
End If
End If
End If
MessageBox.Show("No Match Found.")
End If
End Sub

Comments or Responses

Login to post response