Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a VB.NET Expert!

Join 415,715 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 670 people online right now.Registration is fast and FREE... Join Now!



Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Read a txt file into a Richtextbox Rate Topic: -----

#1 snssewell  Icon User is offline

  • New D.I.C Head
  • Pip

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 15-January 08


Dream Kudos: 0

Share |

Read a txt file into a Richtextbox

Post icon  Posted 15 January 2008 - 10:17 AM

I am trying to read a txt file into a richtextbox and been doing some research and from what I read this should work, but I get a error saying that the file format is not valid. I thought that the richtextbox control can read txt files as well as rtf files automatically. The file I am trying to read is saved in Notepad as a txt file. I can open the file if it is rtf, but not if it is a txt. Any Ideas why it wouldn't work? I am also attaching the textfile I am trying to use. Here is what I got so far.

Option Strict On
Option Explicit On

Public Class frmMain

	Private Sub OpenFile()
		Dim Result As DialogResult
		Result = ofdMain.ShowDialog
		If Result = Windows.Forms.DialogResult.OK Then
			rtbNotes.LoadFile(ofdMain.FileName, RichTextBoxStreamType.RichText)
		End If
		
		tsslFileName.Text = ofdMain.FileName
	End Sub
	Private Sub SaveFile()
		Dim Result As DialogResult
		Result = sfdMain.ShowDialog
		If Result = Windows.Forms.DialogResult.OK Then
			rtbNotes.SaveFile(sfdMain.FileName, RichTextBoxStreamType.RichText)
		End If
	End Sub


	Private Sub NavigateFirst()
		rtbNotes.Selectionstart = 0
		rtbNotes.SelectionLength = 0
	End Sub
	Private Sub NavigateLast()
		rtbNotes.Selectionstart = rtbNotes.Text.Length - 1
	End Sub

	Private Sub msExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msExit.Click
		Me.Close()
	End Sub

	Private Sub msOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msOpen.Click
		Call OpenFile()
	End Sub

	Private Sub msSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles msSave.Click
		Call SaveFile()
	End Sub

	Private Sub tsOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsOpen.Click
		Call OpenFile()
	End Sub

	Private Sub tsSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsSave.Click
		Call SaveFile()
	End Sub
End Class 

Attached File(s)

  • Attached File  Demo.txt (502bytes)
    Number of downloads: 197

This post has been edited by snssewell: 15 January 2008 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • Icon

Reputation: 380
  • View blog
  • Posts: 9,355
  • Joined: 26-December 05


Dream Kudos: 500

Expert In: Everything

Re: Read a txt file into a Richtextbox

Posted 15 January 2008 - 11:28 AM

It can read standard textfiles, but you need to tell the LoadFile method that. Currently you are telling it to load a file with an RTF extension, not a TXT extension. You just need to change the second parameter to PlainText instead of RichText.

Change it too:
rtbNotes.LoadFile(ofdMain.FileName, RichTextBoxStreamType.PlainText)

Was This Post Helpful? 2

#6 Guest_Kludgemaster*


Reputation:

Re: Read a txt file into a Richtextbox

Posted 10 February 2010 - 11:08 AM

Thanks!
Was This Post Helpful? 0

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users