Welcome to Dream.In.Code
Become a VB.NET Expert!

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




Read a txt file into a Richtextbox

 
Reply to this topicStart new topic

Read a txt file into a Richtextbox

snssewell
15 Jan, 2008 - 10:17 AM
Post #1

New D.I.C Head
*

Joined: 15 Jan, 2008
Posts: 3


My Contributions
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.

CODE
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


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


Attached File(s)
Attached File  Demo.txt ( 502bytes ) Number of downloads: 73
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Read A Txt File Into A Richtextbox
15 Jan, 2008 - 11:28 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
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:
CODE

rtbNotes.LoadFile(ofdMain.FileName, RichTextBoxStreamType.PlainText)

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 04:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month