Link to home
Start Free TrialLog in
Avatar of vlvawter
vlvawter

asked on

How do I display a file

I'm puzzled how to display a Word file on a window form.  I know I can create an instance of Word and display a file with Word, but all I want is a simple read-only window for the user to scroll through.  Is there a way to have an enlarged text box with a scroll bar for the user to look at a file?  That's all I really want (or something that would look like that).

Can you help?  Thanks.

Avatar of tsay
tsay

Hey,

you can read the wordfile as any other file with the StreamReader object. Then you can display it in a RichTextBox. You can play with the RichTextBox properties on how it should handle scrollbars etc. I'll provide you with an example code that reads a file called "file.doc" into a richtextbox on a form. Good luck with it.

----EXAMPLE CODE----
fs=New FileStream("file.doc",FileMode.Open,FileAccess.Read) 'file.doc can be, offcourse, any file you wish
'declaring a FileStream to open the file named file.doc with access mode of reading
Dim d as new StreamReader(fs)
'creating a new StreamReader and passing the filestream object fs as argument
d.BaseStream.Seek(0,SeekOrigin.Begin)
'Seek method is used to move the cursor to different positions in a file, in this code, to
'the beginning
while d.peek()>-1
'peek method of StreamReader object tells how much more data is left in the file
RichTextbox1.Text &= d.readLine()
'displaying text from doc file in the RichTextBox
End while
d.close()
----EXAMPLE CODE----
Avatar of Mike Tomlinson
tsay...how could that possibly work?

A Word document is NOT a plain text file.  All the control characters embedded in the document that tell MSWord how to format that document would prevent it from being displayed properly.  The only way your code would work was if "file.doc" was actually a ".txt" file that has been renamed to a ".doc" file...
ASKER CERTIFIED SOLUTION
Avatar of bman9111
bman9111

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial