2 Replies - 21321 Views - Last Post: 10 February 2010 - 12:08 PM Rate Topic: -----

#1 snssewell  Icon User is offline

  • New D.I.C Head

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

Read a txt file into a Richtextbox

Post icon  Posted 15 January 2008 - 11: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: 665

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


Is This A Good Question/Topic? 0
  • +

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Read a txt file into a Richtextbox

Posted 15 January 2008 - 12:28 PM

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 - 12:08 PM

Thanks!
Was This Post Helpful? 0

Page 1 of 1