SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var start = MyRichTextBox.Document.ContentStart;
  2. var startPos = start.GetPositionAtOffset(3);
  3. var endPos = start.GetPositionAtOffset(8);
  4. var textRange = new TextRange(startPos,endPos);
  5. textRange.ApplyPropertyValue(TextElement.ForegroundProperty,
  6.     new SolidColorBrush(Colors.Blue));
  7. textRange.ApplyPropertyValue(TextElement.FontWeightProperty,
  8.     FontWeights.Bold);
  9.      
  10. private static TextPointer GetPoint(TextPointer start, int x)
  11. {
  12.     var ret = start;
  13.     var i = 0;
  14.     while (i < x && ret != null)
  15.     {
  16.         if (ret.GetPointerContext(LogicalDirection.Backward) ==
  17. TextPointerContext.Text ||
  18.             ret.GetPointerContext(LogicalDirection.Backward) ==
  19. TextPointerContext.None)
  20.             i++;
  21.         if (ret.GetPositionAtOffset(1,
  22. LogicalDirection.Forward) == null)
  23.             return ret;
  24.         ret = ret.GetPositionAtOffset(1,
  25. LogicalDirection.Forward);
  26.     }
  27.     return ret;
  28. }
  29.      
  30. Colorize(item.Offset, item.Text.Length, Colors.Blue);
  31.  
  32. private void Colorize(int offset, int length, Color color)
  33. {
  34.     var textRange = MyRichTextBox.Selection;
  35.     var start = MyRichTextBox.Document.ContentStart;
  36.     var startPos = GetPoint(start, offset);
  37.     var endPos = GetPoint(start, offset + length);
  38.  
  39.     textRange.Select(startPos, endPos);
  40.     textRange.ApplyPropertyValue(TextElement.ForegroundProperty,
  41. new SolidColorBrush(color));
  42.     textRange.ApplyPropertyValue(TextElement.FontWeightProperty,
  43. FontWeights.Bold);
  44. }
  45.      
  46. var textRange = MyRichTextBox.Selection;
  47. var start = MyRichTextBox.Document.ContentStart;
  48. var startPos = start.GetPositionAtOffset(3);
  49. var endPos = start.GetPositionAtOffset(8);
  50. textRange.Select(startPos, endPos);
  51. textRange.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
  52. textRange.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
  53.      
  54. private static TextPointer GetPoint(TextPointer start, int x)
  55. {
  56.  
  57.         var ret = start;
  58.         var i = 0;
  59.         while (ret != null)
  60.         {
  61.             string stringSoFar = new TextRange(ret, ret.GetPositionAtOffset(i, LogicalDirection.Forward)).Text;
  62.             if (stringSoFar.Length == x)
  63.                     break;
  64.             i++;
  65.             if (ret.GetPositionAtOffset(i, LogicalDirection.Forward) == null)
  66.                 return ret.GetPositionAtOffset(i-1, LogicalDirection.Forward)
  67.  
  68.         }
  69.         ret=ret.GetPositionAtOffset(i, LogicalDirection.Forward);
  70.         return ret;
  71. }
  72.      
  73. <Grid FocusManager.IsFocusScope="True">...</Grid>
  74.      
  75. private void Colorize(int offset, int length, Color color)
  76. {
  77.     var textRange = MyRichTextBox.Selection;
  78.     var start = MyRichTextBox.Document.ContentStart;
  79.     var startPos = GetPoint(start, offset);
  80.     var endPos = GetPoint(start, offset + length);
  81.  
  82.     textRange.Select(startPos, endPos);
  83. }
  84.      
  85. <RichTextBox FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}">...</RichTextBox>
  86.      
  87. private static TextPointer GetPositionAtCharOffset(TextPointer start, int numbertOfChars)
  88. {
  89.     var offset = start;
  90.     int i = 0;
  91.     string stringSoFar="";
  92.     while (stringSoFar.Length < numbertOfChars)
  93.     {
  94.         i++;
  95.         TextPointer offsetCandidate = start.GetPositionAtOffset(
  96.                 i, LogicalDirection.Forward);
  97.  
  98.         if (offsetCandidate == null)
  99.             return offset; // ups.. we are to far
  100.  
  101.         offset = offsetCandidate;
  102.         stringSoFar = new TextRange(start, offset).Text;
  103.     }
  104.  
  105.     return offset;
  106. }
  107.      
  108. stringSoFar = stringSoFar.Replace("rn", "")
  109.                          .Replace(" ", "")
  110.      
  111. var startPos = GetPoint(start, offset);
  112. var endPos = GetPoint(start, offset + length);
  113.      
  114. var startPos = GetPoint(start, offset);
  115. var endPos = GetPoint(startPos, length);
  116.      
  117. private static TextRange GetTextRange(TextPointer start, int startIndex, int length)
  118. {
  119.     var rangeStart = GetPositionAtCharOffset(start, startIndex);
  120.     var rangeEnd = GetPositionAtCharOffset(rangeStart, length);
  121.     return new TextRange(rangeStart, rangeEnd);
  122. }
  123.      
  124. var range = GetTextRange(Document.ContentStart, 3, 8);
  125. range.ApplyPropertyValue(
  126.     TextElement.BackgroundProperty,
  127.     new SolidColorBrush(Colors.Aquamarine));
  128.      
  129. TextPointer startPos = rtb.Document.ContentStart.GetPositionAtOffset(searchWordIndex, LogicalDirection.Forward);
  130. startPos = startPos.CorrectPosition(searchWord, FindDialog.IsCaseSensitive);
  131. if (startPos != null)
  132. {
  133.     TextPointer endPos = startPos.GetPositionAtOffset(textLength, LogicalDirection.Forward);
  134.     if (endPos != null)
  135.     {
  136.          rtb.Selection.Select(startPos, endPos);
  137.     }
  138. }
  139.  
  140. public static TextPointer CorrectPosition(this TextPointer position, string word, bool caseSensitive)
  141. {
  142.    TextPointer start = null;
  143.    while (position != null)
  144.    {
  145.        if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
  146.        {
  147.            string textRun = position.GetTextInRun(LogicalDirection.Forward);
  148.  
  149.            int indexInRun = textRun.IndexOf(word, caseSensitive ? StringComparison.InvariantCulture : StringComparison.InvariantCultureIgnoreCase);
  150.            if (indexInRun >= 0)
  151.            {
  152.                start = position.GetPositionAtOffset(indexInRun);
  153.                break;
  154.            }
  155.        }
  156.  
  157.        position = position.GetNextContextPosition(LogicalDirection.Forward);
  158.    }
  159.  
  160.    return start;
  161. }
  162.      
  163. private TextPointer GetPoint(TextPointer start, int pos)
  164.     {
  165.         var ret = start;
  166.         int i = 0;
  167.         while (i < pos)
  168.         {
  169.             if (ret.GetPointerContext(LogicalDirection.Forward) ==
  170.     TextPointerContext.Text)
  171.                 i++;
  172.             if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
  173.                 return ret;
  174.             ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
  175.         }
  176.         return ret;
  177.     }
  178.      
  179. private void SelectText(int start, int length)
  180.     {
  181.         TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
  182.         TextPointer pointerStart = textRange.Start.GetPositionAtOffset(start, LogicalDirection.Forward);
  183.         TextPointer pointerEnd = textRange.Start.GetPositionAtOffset(start + length, LogicalDirection.Backward);
  184.  
  185.         richTextBox.Selection.Select(pointerStart, pointerEnd);
  186.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top