2016-07-26 24 views
0

我沒有在用阿拉伯語書寫的文本內的RichBox工具,一個變音符號,並且當您搜索阿拉伯語詞組變音時,標記不會正確地將該單詞着色爲你看:搜索Richbox工具爲阿拉伯語的單詞忽略格式在WPF

enter image description here

但是,如果你搜索在沒有注音符號的話,這將是地板着色正確,你看:

enter image description here

這個搜索代碼,我使用,我覺得現在的問題是,所以我希望你將它修改爲我:

 Dim NumSearch As Integer 
     NumSearch = 0 
     Dim keyword As String = ReplaceString(TxtSearch.Text.Trim) 

     Dim text As New TextRange(RichTxtPost.Document.ContentStart, RichTxtPost.Document.ContentEnd) 
     Dim current As TextPointer = text.Start.GetInsertionPosition(LogicalDirection.Forward) 


     While current IsNot Nothing 
      Dim textInRun As String = ReplaceString(current.GetTextInRun(LogicalDirection.Forward)) 
      'If Not String.IsNullOrWhiteSpace(textInRun) Then 
      If Not String.IsNullOrEmpty(textInRun) Then 
       Dim index As Integer = textInRun.IndexOf(keyword) 
       If index <> -1 Then 
        Dim selectionStart As TextPointer = current.GetPositionAtOffset(index, LogicalDirection.Forward) 
        Dim selectionEnd As TextPointer = selectionStart.GetPositionAtOffset(keyword.Length, LogicalDirection.Forward) 
        Dim selection As New TextRange(selectionStart, selectionEnd) 
        NumSearch = Val(NumSearch) + 1 

        'selection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold) 
        selection.ApplyPropertyValue(TextElement.ForegroundProperty, New SolidColorBrush(Colors.Red)) 

        RichTxtPost.Selection.[Select](selection.Start, selection.[End]) 
        RichTxtPost.Focus() 

       End If 
      End If 
      current = current.GetNextContextPosition(LogicalDirection.Forward) 
     End While 

此替換功能,以取代簡介:

Public Function ReplaceString(In_Text As String) As String 
     'خاص بالبحث مع تجاهل التشكيل 
     Dim X As Long 
     Dim strChar As String 
     Dim strReturn As String 
     strReturn = "" 

     For X = 1 To Len(In_Text) 
      strChar = Mid(In_Text, X, 1) 
      Select Case strChar 
       Case "أ", "إ", "آ" 
        strChar = "ا" 
       Case "ه" 
        strChar = "ة" 
       Case Chr(243), Chr(240), Chr(245), Chr(246), Chr(242), Chr(241), Chr(248), Chr(250) 
        ''حذف علامات التشكيلية إذا وجد، وهي 
        ' 
        '' َ ً ُ ِ ٍ. 
        'strChar = "" 

        ' يجب اضافة حرف وهمي غير مستخدم في النص الاساسي بدلا من كل حرف مطلوب تجاهله سواء همزة او شكله 
        strChar = "" 
      End Select 
      strReturn = strReturn & strChar 
     Next 
     ReplaceString = strReturn 
    End Function 

回答

0

這方案如下:

 Dim range As New TextRange(RichTxtPost.Document.ContentStart, RichTxtPost.Document.ContentEnd) 
     'منسق 
     Dim GetPost As String 
     GetPost = myInput2 
     Dim documentBytes = Encoding.UTF8.GetBytes(GetPost) 
     Using reader = New MemoryStream(documentBytes) 
      reader.Position = 0 
      RichTxtPost.SelectAll() 
      RichTxtPost.Selection.Load(reader, DataFormats.Rtf) 
     End Using 

     Dim pattern As String = "" 

     For Each c As Char In Me.TxtSearch.Text.Trim 
      pattern = pattern & Regex.Escape(c) & "[\u064B-\u0653]*" 
     Next c 

     'للبحث عن كلمة واحدة 
     Dim reg As New Regex("(" & pattern & ")", RegexOptions.Compiled Or RegexOptions.IgnoreCase) 
     'للبحث عن عدة كلمات 
     'Dim reg As New Regex("(" & pattern & "| مُحَمَّدٍ " & "| إِبْرَاهِيمَ " & ")", RegexOptions.Compiled Or RegexOptions.IgnoreCase) 

     Dim start = RichTxtPost.Document.ContentStart 
     While start IsNot Nothing AndAlso start.CompareTo(RichTxtPost.Document.ContentEnd) < 0 
      If start.GetPointerContext(LogicalDirection.Forward) = TextPointerContext.Text Then 
       Dim match = reg.Match(start.GetTextInRun(LogicalDirection.Forward)) 

       Dim textrange = New TextRange(start.GetPositionAtOffset(match.Index, LogicalDirection.Forward), start.GetPositionAtOffset(match.Index + match.Length, LogicalDirection.Backward)) 
       textrange.ApplyPropertyValue(TextElement.ForegroundProperty, New SolidColorBrush(Colors.Red)) 
       'textrange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold) 
       start = textrange.[End] 
      End If 
      start = start.GetNextContextPosition(LogicalDirection.Forward) 
     End While 

謝謝:)