Page 1 of 1

load text 2 richtxtbox

Posted: Wed Mar 23, 2011 12:16 pm
by lesan101
It seems easy but i can't fiqure it out

loading a reguler .txt file into a richtextbox1 with an opendialogbox

also saving it...

Re: load text 2 richtxtbox

Posted: Wed Mar 23, 2011 12:22 pm
by Skillful
Open File:
Code: Select all
Try
            Dim cf As String
            OpenFileDialog1.Title = "RTE - Open File"
            OpenFileDialog1.DefaultExt = "rtf"
            OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|Rich Text Files (*.rtf)|*.rtf|HTML Files (*.htm,*.html)|*.htm,*.html|VB Files (*.vb)|*.vb|PHP Files (*.php)|*.php|All Files (*.*)|*.*"
            OpenFileDialog1.FilterIndex = 1
            OpenFileDialog1.ShowDialog()
            Label1.Text = OpenFileDialog1.SafeFileName
            Label2.Text = OpenFileDialog1.FileName
            If OpenFileDialog1.FileName = "" Then Exit Sub
            Dim strExt As String
            strExt = System.IO.Path.GetExtension(OpenFileDialog1.FileName)
            strExt = strExt.ToUpper()
            Select Case strExt
                Case ".RTF"
                    RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.RichText)
                Case Else
                    Dim txtReader As System.IO.StreamReader
                    txtReader = New System.IO.StreamReader(OpenFileDialog1.FileName)
                    RichTextBox1.Text = txtReader.ReadToEnd
                    txtReader.Close()
                    txtReader = Nothing
                    RichTextBox1.SelectionStart = 0
                    RichTextBox1.SelectionLength = 0
            End Select
            cf = OpenFileDialog1.FileName
            RichTextBox1.Modified = False
        Catch EX As Exception
            MsgBox("Unidentified Error!!", MsgBoxStyle.Critical)
        End Try
Save As:
Code: Select all
 SaveFileDialog1.Title = "RTE - Save File"
        SaveFileDialog1.DefaultExt = "rtf"
        SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|Rich Text Files (*.rtf)|*.rtf|HTML Files (*.htm,*.html)|*.htm,*.html|All Files (*.*)|*.*"
        SaveFileDialog1.FilterIndex = 1
        SaveFileDialog1.ShowDialog()

        If SaveFileDialog1.FileName = "" Then Exit Sub

        Dim strExt As String
        strExt = System.IO.Path.GetExtension(SaveFileDialog1.FileName)
        strExt = strExt.ToUpper()

        Select Case strExt
            Case ".RTF"
                RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.RichText)
            Case ".TXT"
                Dim confirm As Integer
                confirm = MsgBox("All formatting will be lost if you save it in the .txt format. You should save the file in the .rtf format to save the file with the formatting. Would you like to continue saving the file in the .txt format?", vbYesNo + vbQuestion)
                If confirm = vbYes Then
                    Dim txtWriter As System.IO.StreamWriter
                    txtWriter = New System.IO.StreamWriter(SaveFileDialog1.FileName)
                    txtWriter.Write(RichTextBox1.Text)
                    txtWriter.Close()
                    txtWriter = Nothing
                    RichTextBox1.SelectionStart = 0
                    RichTextBox1.SelectionLength = 0
                Else
                    SaveFileDialog1.ShowDialog()
                End If
            Case Else
                Dim txtWriter As System.IO.StreamWriter
                txtWriter = New System.IO.StreamWriter(SaveFileDialog1.FileName)
                txtWriter.Write(RichTextBox1.Text)
                txtWriter.Close()
                txtWriter = Nothing
                RichTextBox1.SelectionStart = 0
                RichTextBox1.SelectionLength = 0
        End Select

        cf = SaveFileDialog1.FileName
        RichTextBox1.Modified = False
Save File:
Code: Select all
Dim cf As String
        If cf = "" Then
            SaveAsToolStripMenuItem.PerformClick()
            Exit Sub
        End If
        Dim strExt As String
        strExt = System.IO.Path.GetExtension(cf)
        strExt = strExt.ToUpper()
        Select Case strExt
            Case ".RTF"
                RichTextBox1.SaveFile(cf)
            Case Else
                Dim txtWriter As System.IO.StreamWriter
                txtWriter = New System.IO.StreamWriter(cf)
                txtWriter.Write(RichTextBox1.Text)
                txtWriter.Close()
                txtWriter = Nothing
                RichTextBox1.SelectionStart = 0
                RichTextBox1.SelectionLength = 0
                RichTextBox1.Modified = False
        End Select
Please +rep if this helps cooll;

Re: load text 2 richtxtbox

Posted: Wed Mar 23, 2011 12:34 pm
by lesan101
awesome +Rep'd :D

Re: load text 2 richtxtbox

Posted: Wed Mar 23, 2011 12:39 pm
by Skillful
lesan101 wrote:
awesome +Rep'd :D
Actually this was the code I used in my Notepad app.
So I just copy+pasted the from my app!
Anyways thanks for the +rep!