This is a migrated thread and some comments may be shown as answers.

Initialize RichTextBox with docx file

9 Answers 315 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Terry
Top achievements
Rank 1
Terry asked on 10 Aug 2010, 08:55 PM
I am trying to do something that I think should be pretty simple, but I'm not having any success.  

I want to display a static docx file using a RichTextBox in read only mode in a silverlight 4 user control running on a web site.  This is just to display a legal disclaimer on a site. The file exists on the web server, not on the individual's local machine.  I'm using VS 2010.

I looked at the examples and I tried using the code from the Document API example for the RichTextBox control.  Every time I try to create a Stream object that can be used with the DocxFormatProvider Import method, I get the error that Null exception has occurred. 

Is there a sample that shows how to load a docx file from the server (not using the client OpenFileDialog function)?

9 Answers, 1 is accepted

Sort by
0
Ivailo Karamanolev
Telerik team
answered on 12 Aug 2010, 01:15 PM
Hello Terry,

I have attached a demo project which loads a docx from the server into a RadRichTextBox. The docx is loaded from the ClientBin directory of the Web project and I have used WebClient to do that. I hope this helps you. Let us know if you need further assistance.

Sincerely yours,
Ivailo
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Mike
Top achievements
Rank 2
answered on 20 Aug 2010, 04:35 PM
Any chance that someone has (or can) converted this project to VB?

If I download the project and run it, it works just exactly like what I want.

I've tried to convert it to VB for my needs, but am having trouble with the "webClient.OpenReadcompleted" line.  I get an error message that says: "Public Event OpenReadCompleted(sender As Object, e As System.Net.OpenReadCompletedEventARgs)" is an event, and cannot be called directly.  Use a 'RaiseEvent' statement to raise an event.

Basically, here's what I'm trying to accomplish:
1)  I'm developing a SilverLight website for my client using Telerik controls using SilverLight's Navigation structure.
2)  Client has a MSWord document that describes their Guidelines.
3)  I want to upload that that document to a folder on the website and have it display in a RichTextBox on one of the pages when the user navigates to the page.
4)  The guidelines can change from year-to-year, so I want to just be able to upload a new verison of the .docx file to the folder on the host whenever the document changes.

Is this project the correct approach? 
If so, hopefully I can get it converted to VB and working with my project and be on my merry way!

Thanks in advance,
Mike T.
0
Terry
Top achievements
Rank 1
answered on 20 Aug 2010, 05:16 PM
Mike,

Here's a VB version of the project that Ivailo supplied to me....

Actually, I was trying to supply a vb version, but I can't attach a zip file.  The main issue you are probably having is in the MainPage.xaml.vb file because VB handles events differenly and C#.  Here is that file:

Imports

 

 

System

 

Imports

 

 

System.Net

 

Imports

 

 

System.Windows.Controls

 

Imports

 

 

Telerik.Windows.Documents.FormatProviders.OpenXml.Docx

 

Partial

 

 

Public Class MainPage

 

 

 

Inherits UserControl

 

 

 

Private WithEvents webClient As WebClient

 

 

 

Public Sub New()

 

InitializeComponent()

webClient =

 

New WebClient

 

webClient.OpenReadAsync(

 

New Uri("Rad-RichTextBox-for-Silverlight.docx", UriKind.Relative))

 

 

 

End Sub

 

 

 

Private Sub webClient_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs) Handles webClient.OpenReadCompleted

 

richTextBox.Document =

 

New DocxFormatProvider().Import(e.Result)

 

e.Result.Close()

 

 

End Sub

 

End

 

 

Class

 

0
Terry
Top achievements
Rank 1
answered on 20 Aug 2010, 05:18 PM
Mike,

Let me try that again.  Pasting the code looked fine when I was submitting, but it's unreadable as it display here.  I'll try with just plain text:

Imports System
Imports System.Net
Imports System.Windows.Controls
Imports Telerik.Windows.Documents.FormatProviders.OpenXml.Docx
Partial Public Class MainPage
    Inherits UserControl
    Private WithEvents webClient As WebClient
    Public Sub New()
        InitializeComponent()

        webClient = New WebClient
        webClient.OpenReadAsync(New Uri("Rad-RichTextBox-for-Silverlight.docx", UriKind.Relative))
    End Sub

    Private Sub webClient_OpenReadCompleted(ByVal sender As Object, ByVal e As OpenReadCompletedEventArgs) Handles webClient.OpenReadCompleted
        richTextBox.Document = New DocxFormatProvider().Import(e.Result)
        e.Result.Close()
    End Sub
End Class

0
Mike
Top achievements
Rank 2
answered on 20 Aug 2010, 06:38 PM
Terry,
Thanks a ton for your reply.  That did get rid of errors that I was having.

The page displays, and I put borders around the richtextbox control so that I could see it. 
BUT, the richtextbox is empty, no text displays.

This is the line with the filename:
webClient.OpenReadAsync(New Uri("guidelines.docx"UriKind.Relative))

I know that file exists.  Do I need to qualify it more with some other kind of path details?

The only other difference I see is that your code "Inherits UserControl" and mine "Inherits Page" because I'm using navigation pages.
That wouldn't matter would it?

Does anything need to change in the .xaml?

Here's my declaration for the richtextbox control:
<telerik:RadRichTextBox Name="radRichTextBox1" Width="700" Background="White" BorderThickness="1" BorderBrush="Black" IsReadOnly="True" />


Thanks again, a ton, for your help!
0
Terry
Top achievements
Rank 1
answered on 20 Aug 2010, 07:37 PM
Mike,

Did you put the guidelines.docx file in the ClientBin folder?
0
Mike
Top achievements
Rank 2
answered on 20 Aug 2010, 10:51 PM
Doh!  At first I didn't.

But, even after putting it there, it still doesn't show.

I did notice that when I run the project from VS 2010, if the file is not in the ClientBin folder, the status bar on FireFox says "Waiting on Localhost...".  If the file is there, it says: "Transferring data from Localhost...", but nothing ever shows up.

0
Mike
Top achievements
Rank 2
answered on 22 Aug 2010, 11:50 PM

Terry,
Thanks so much for your help.  I finally figured out what my problem was.

For some reason, the "Handles webClient.OpenReadCompleted" statement was missing from the OpenReadCompleted() event.

Added that and it started working perfectly.

 

Thanks again for all of your help!

 

Mike T.

0
Mike
Telerik team
answered on 24 Aug 2010, 08:14 AM
Thanks Terry,
Mike, we are very glad you resolved this issue!
Let us know if anything else pops up.

Greetings,
Mike
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
RichTextBox
Asked by
Terry
Top achievements
Rank 1
Answers by
Ivailo Karamanolev
Telerik team
Mike
Top achievements
Rank 2
Terry
Top achievements
Rank 1
Mike
Telerik team
Share this question
or