WPF RichTextBox bug

S

Sergey

I need a help to avoid WPF RichTextBox bug. I develop an application using
NET Framework 3.5 SP 1. My application contains RichTextBox and provides
Print Preview feature. I found that RichTextBox.GetPositionFromPoint()
method
raises an exception after RichTextBox.Document.DocumentPaginator call, and
RichTextBox stops working. The exception is raised inside
MS.Internal.Documents.DocumentPageTextView.Validate() method. The problem
easy reproduces by the following code (it also occurs in test project
containing RichTextBox only):

IDocumentPaginatorSource paginatorSource =
(IDocumentPaginatorSource)theRichTextBox.Document;
DocumentPaginator paginator = paginatorSource.DocumentPaginator;
theRichTextBox.GetPositionFromPoint(new Point(1, 1), true);

I've tried to make a copy of the document using TextRange.Save(stream,
DataFormats.Xaml) and TextRange.Load(stream, DataFormats.Xaml) and ask the
paginator from the copy. In this case the exception does not occurs, but all
images contained in the document are lost.
Can someone help me to avoid this problem, please?

Sincerely,
Sergey Zyryanov
 
P

Pavel Minaev

I need a help to avoid WPF RichTextBox bug. I develop an application using
NET Framework 3.5 SP 1. My application contains RichTextBox and provides
Print Preview feature. I found that RichTextBox.GetPositionFromPoint()
method
raises an exception after RichTextBox.Document.DocumentPaginator call, and
RichTextBox stops working. The exception is raised inside
MS.Internal.Documents.DocumentPageTextView.Validate()  method. The problem
easy reproduces by the following code (it also occurs in test project
containing RichTextBox only):

IDocumentPaginatorSource paginatorSource =
(IDocumentPaginatorSource)theRichTextBox.Document;
DocumentPaginator paginator = paginatorSource.DocumentPaginator;
theRichTextBox.GetPositionFromPoint(new Point(1, 1), true);

I've tried to make a copy of the document using TextRange.Save(stream,
DataFormats.Xaml) and TextRange.Load(stream, DataFormats.Xaml) and ask the
paginator from the copy. In this case the exception does not occurs, but all
images contained in the document are lost.
Can someone help me to avoid this problem, please?

Try saving using DataFormats.XamlPackage - that should keep the
images.

As for the RichTextBox bug itself, please report it at the bug
tracker:
https://connect.microsoft.com/VisualStudio/feedback
 
S

Sergey

Thank you very much, it works!

I've posted the report bug as you suggested.

Sincerely,
Sergey Zyryanov

I need a help to avoid WPF RichTextBox bug. I develop an application using
NET Framework 3.5 SP 1. My application contains RichTextBox and provides
Print Preview feature. I found that RichTextBox.GetPositionFromPoint()
method
raises an exception after RichTextBox.Document.DocumentPaginator call, and
RichTextBox stops working. The exception is raised inside
MS.Internal.Documents.DocumentPageTextView.Validate() method. The problem
easy reproduces by the following code (it also occurs in test project
containing RichTextBox only):

IDocumentPaginatorSource paginatorSource =
(IDocumentPaginatorSource)theRichTextBox.Document;
DocumentPaginator paginator = paginatorSource.DocumentPaginator;
theRichTextBox.GetPositionFromPoint(new Point(1, 1), true);

I've tried to make a copy of the document using TextRange.Save(stream,
DataFormats.Xaml) and TextRange.Load(stream, DataFormats.Xaml) and ask the
paginator from the copy. In this case the exception does not occurs, but
all
images contained in the document are lost.
Can someone help me to avoid this problem, please?

Try saving using DataFormats.XamlPackage - that should keep the
images.

As for the RichTextBox bug itself, please report it at the bug
tracker:
https://connect.microsoft.com/VisualStudio/feedback
 
H

Hongye Sun [MSFT]

Thanks Pavel for your accurate answer.

Hi Sergey,

Welcome to Microsoft Newsgroup Service. I am Hongye Sun [MSFT] and it is my
pleasure to work with you on this issue.

Your intension to make a copy of the FlowDocument using TextRange Save and
TextRange.Load is the right direction to this issue. The only thing you
missing here is what Pavel has said: change from DataFormats.Xaml to
DataFormats.XamlPackage, which supports both text and images. (Note:
XamlPackage format isn't supported in partial trust.)

For clarification, here is a sample code:
-----------------------------------------------------------
// Serialize RichTextBox content into a stream in XamlPackage format.
TextRange sourceDocument = new
TextRange(theRichTextBox.Document.ContentStart,
theRichTextBox.Document.ContentEnd);
MemoryStream stream = new MemoryStream();
sourceDocument.Save(stream, DataFormats.XamlPackage);

// Clone the source document's content into a new FlowDocument.
FlowDocument flowDocumentCopy = new FlowDocument();
TextRange copyDocumentRange = new TextRange(flowDocumentCopy.ContentStart,
flowDocumentCopy.ContentEnd);
copyDocumentRange.Load(stream, DataFormats.XamlPackage);

// Get paginator from copy document
IDocumentPaginatorSource paginatorSource =
(IDocumentPaginatorSource)flowDocumentCopy;
DocumentPaginator paginator = paginatorSource.DocumentPaginator;
-----------------------------------------------------------
Please let me know if the code works for you.

You might wonder why we need to copy the original RichTextBox's
FlowDocument and then send the cloned FlowDocument's paginator to printer.
This is necessary because there is no way to support simultaneous editing
and printing of the original FlowDocument in RichTextBox. This explanation
is from member of product group.

After getting the DocumentPaginator from FlowDocument, the RichTextBox
cannot be used unless it is re-initialized. If you comment your orginal
last code to prevent the null reference exception, you will find that after
getting DocumentPaginator code is executed, the content in RichTextBox is
disappeared and you cannot edit it anymore. This could be a bug or by
design issue. The right process, as Pavel mentioned, is to report it at
http://connect.microsoft.com/. Through this channel, the issue will be
processed by product group and it will be recorded in our internal database.

If you still need any help on it, please do not hesitate to contact me. I
will be more than happy to support you.

Have a great day!

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within?2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

WPF RichTextBox Control 1

Top