Guest User

Untitled

a guest
Oct 19th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. new TextRange(Document.ContentStart, Document.ContentEnd).IsEmpty
  2.  
  3. var start = rtb.Document.ContentStart;
  4. var end = rtb.Document.ContentEnd;
  5. int difference = start.GetOffsetToPosition(end);
  6.  
  7. public bool IsRichTextBoxEmpty(RichTextBox rtb)
  8. {
  9. if (rtb.Document.Blocks.Count == 0) return true;
  10. TextPointer startPointer = rtb.Document.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);
  11. TextPointer endPointer = rtb.Document.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
  12. return startPointer.CompareTo(endPointer) == 0;
  13. }
  14.  
  15. bool IsEmpty(Document document)
  16. {
  17. string text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
  18. if (string.IsNullOrWhiteSpace(text) == false)
  19. return false;
  20. else
  21. {
  22. if (document.Blocks.OfType<BlockUIContainer>()
  23. .Select(c => c.Child).OfType<Image>()
  24. .Any())
  25. return false;
  26. }
  27. return true;
  28. }
  29.  
  30. string text = new TextRange(Document.ContentStart, Document.ContentEnd).Text;
  31. return !String.IsNullOrWhiteSpace(text);
  32.  
  33. Private Function RichTextBoxIsEmpty(BYVAL rtb As RichTextBox) As Boolean
  34.  
  35. Dim ReturnCode As Boolean = True
  36.  
  37. Dim text As String = New TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text
  38.  
  39. If String.IsNullOrWhiteSpace(text) Then
  40.  
  41. For Each block As Block In rtb.Document.Blocks
  42.  
  43. 'check for an image
  44. If TypeOf block Is Paragraph Then
  45. Dim paragraph As Paragraph = DirectCast(block, Paragraph)
  46. For Each inline As Inline In paragraph.Inlines
  47. If TypeOf inline Is InlineUIContainer Then
  48. Dim uiContainer As InlineUIContainer = DirectCast(inline, InlineUIContainer)
  49. If TypeOf uiContainer.Child Is Image Then
  50. ReturnCode = False
  51. Exit For
  52. End If
  53. End If
  54. Next
  55. End If
  56.  
  57. ' Check for a table
  58. If TypeOf block Is Table Then
  59. ReturnCode = False
  60. Exit For
  61. End If
  62.  
  63. Next
  64.  
  65. Else
  66.  
  67. ReturnCode = False
  68.  
  69. End If
  70.  
  71. Return ReturnCode
  72.  
  73. End Function
  74.  
  75. var rtf = GetRtfText();
  76.  
  77. var start = Document.ContentStart;
  78. var end = Document.ContentEnd;
  79. var difference = start.GetOffsetToPosition(end);
  80.  
  81. HasText = difference > 4 || rtf.Length > 350;
  82.  
  83.  
  84. public string GetRtfText()
  85. {
  86. var tr = new TextRange(Document.ContentStart, Document.ContentEnd);
  87. using (var ms = new MemoryStream())
  88. {
  89. tr.Save(ms, DataFormats.Rtf);
  90. return Encoding.Default.GetString(ms.ToArray());
  91. }
  92. }
Add Comment
Please, Sign In to add comment