How do I open .rtf and .txt, depending on user selection.  
Author Message
NeW2VB





PostPosted: Visual Basic Express Edition, How do I open .rtf and .txt, depending on user selection. Top

I have a richtextbox on my form and i want to allow the use to select either .txt or .rtf files to open in the richtextbox, but no matter how I seem to code it, I can only open either .rtf or .txt depending on how I write my code Can someone please help me with this

If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

openFileName = OpenFileDialog1.FileName

' open stream, read and write file

Try

Dim s As Stream = OpenFileDialog1.OpenFile()

RichTextBox1.LoadFile(s, RichTextBoxStreamType.PlainText)

s.Close()

fileOpened = True

'Display error if file could not be loaded

Catch exp As Exception

MessageBox.Show("An error occurred while attempting to load the file. The error is:" _

+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine)

End Try

fileOpened = False

' open stream, read and write file

Try

Dim s As Stream = OpenFileDialog1.OpenFile()

RichTextBox1.LoadFile(s, RichTextBoxStreamType.PlainText)

s.Close()

fileOpened = True

Catch exp As Exception

MessageBox.Show("An error occurred while attempting to load the file. The error is:" _

+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine)

End Try



Visual Studio Express Editions30  
 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, How do I open .rtf and .txt, depending on user selection. Top

well you are specifying to open it as plaintext document all the time.

Best thing to do would be to check the extension of the file, but even then it would not be a 100% proper solution, as you could rename a .txt into a .rtf which *may* cause an error when loading into the RTF. You also dont need to use a stream at all, you can just directly give the filename/path into the LoadFile() method of the RTB control

however if you are still interested, try this:

 

If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then

Try

If OpenFileDialog1.FileName.EndsWith(".txt") then

   RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.PlainText)

else if OpenFileDialog1.FileName.EndsWith(".rtf") then

   RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.RichText)

end if

  

Catch exp As Exception

MessageBox.Show("An error occurred while attempting to load the file. The error is:" _

+ System.Environment.NewLine + exp.ToString() + System.Environment.NewLine)

 

End Try

 

does this help



 
 
NeW2VB





PostPosted: Visual Basic Express Edition, How do I open .rtf and .txt, depending on user selection. Top

Thank you much!!!!

thats exactly what I needed, works like a charm!

Actually in the code I pasted, the first

Try

Dim s As Stream = OpenFileDialog1.OpenFile()

RichTextBox1.LoadFile(s, RichTextBoxStreamType.PlainText)

s.Close()

should have been

Try

Dim s As Stream = OpenFileDialog1.OpenFile()

RichTextBox1.LoadFile(s, RichTextBoxStreamType.RichText)

s.Close()

but regardless it didn't work.

Thanks Ahmedilyas for the quick reply, your code works beautiffuly!!!!!!


 
 
ahmedilyas





PostPosted: Visual Basic Express Edition, How do I open .rtf and .txt, depending on user selection. Top

Glad I could help!