【发布时间】:2015-09-14 22:50:25
【问题描述】:
我正在尝试设计一个应用程序,如果该字符串中包含另外 1 或 2 个字符串,则该应用程序会删除该字符串上的某些字符。
到目前为止,我似乎可以让前导子字符串相当可靠地完成它的工作,但是第二个子字符串似乎总是产生不可预测的结果,有人可以帮忙吗? (c#或vb都可以)
一个屏幕截图示例,它适用于主(第一个)过滤器,但不适用于子(第二个):
代码如下:
Public Function FormatFilteredName(mainFilter As String, subFilter As String) As TextBlock
Dim tbNew As New TextBlock(New Run(FullFileName))
tbNew.Text = FullFileName
tbNew.FontSize = FONT_SIZE
If Not String.IsNullOrEmpty(FullFileName) Then
If Not String.IsNullOrEmpty(mainFilter) Then
GetFilterSpan(mainFilter, tbNew)
End If
If Not String.IsNullOrEmpty(subFilter) Then
GetFilterSpan(subFilter, tbNew)
End If
End If
Return tbNew
Private Function GetFilterSpan(filter As String, ByVal tbNew As TextBlock) As Span
Dim offset As Integer = tbNew.Text.ToLower().IndexOf(filter.ToLower()) + 1
Try
If offset > -1 Then
Dim tpStart As TextPointer
tpStart = tbNew.ContentStart.GetPositionAtOffset(offset)
Dim tpEnd As TextPointer
tpEnd = tbNew.ContentStart.GetPositionAtOffset(offset + filter.Length)
If Not tpStart Is Nothing And Not tpEnd Is Nothing Then
Dim result As New Span(tpStart, tpEnd)
result = ApplySpanStrikeOutStyle(result)
Return result
End If
End If
Catch ex As Exception
Return Nothing
End Try
【问题讨论】:
-
哈,老乔博客。
标签: c# .net wpf vb.net textblock