Other way of saving

I'm trying to save text from a Rich Textbox to an external .txt file, but I don't know how, can somebody help me


Answer this question

Other way of saving

  • Yabadabadooo

    Thank you very much, my XXL Editor 1.0 is now finished. I'm now working on XXL Editor 1.1

  • Me_Alpy

    just simply reference the rich text box control, give it the filename to save as and set the RichTextBoxStream type as shown.

    http://msdn2.microsoft.com/en-us/library/8bf5hy2e(vs.80).aspx

    there is also an example included above

    if you are asking on prompting the user on which filename and directory to save as, you need to use a SaveFileDialog to do this then save the file. Small example:

     

    Dim theSaveFileDialog as new SaveFileDialog()

    theSaveFileDialog.Filter = "set filters here"

    if theSaveFileDialog.ShowDialog() = System.Windows.Forms.DialogResult.OK then

       'user wants to save

       Me.theRichTextBoxControl.SaveFile(theSaveFileDialog.FileName, RichTextBoxStreamType.PlainText)

    end if

     

    this will prompt the user to save the file and show the save dialog box and if the user presses OK it will then save the text to the filename and path specified.

    More information on the save file dialog:

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx

     



  • Robert Barnes

    RTB.SaveFile( FileSpec, RichTextBoxStreamType.RichText)



  • dafin0

    as shown in my example and from what you have done, the filename/path is in the FileName property of the SaveFileDialog(). you could declare a string variable globally which holds the filename/path to save as but then be sure to make this empty when creating a new document and check to see if the string is empty, if so, then show the dialog else save it to the filename/path stored in this variable, which is almost what you have done by that boolean value.

    declare this globally:

    Dim fileToSaveAs as String = String.Empty

    Sub Save_File()
    Dim Save As New SaveFileDialog()
    Dim MyStreamWriter As System.IO.StreamWriter
    If NewFile = True Then
    Save.Filter = "Plain Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    Save.CheckPathExists = True
    Save.Title = "Save"
    if Save.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK then


    Try
    MyStreamWriter = System.IO.File.AppendText(Save.FileName)
    MyStreamWriter.Write(Text_Place.Text)
    MyStreamWriter.Flush()
    Saved = True
    NewFile = False
    'Here must be the setting of the path to the selected path

    Me.fileToSaveAs = Save.FileName
    Catch ex As Exception
    'Do nothing
    End Try
    Else
    'This is the place where the code for saving to the path for the already saved file

    Me.theRichTextBox.SaveFile(Me.fileToSaveAs, RichTextBoxStreamType.PlainText)
    End If

    end if
    End Sub

    be sure that when they press the new file button to make the fileToSaveAs variable set to String.Empty



  • sumit kr

    I'm just a starter on Visual Basic Express, so I don't know what do fill in, where can I fill in the file directory, and where can I fill in wich value to save

  • remedios_

    no worries - lol. From 1.0 to 1.1 in a space of a few hours

  • pdurbha

    Thank you for your help, but I now found a other way for saving, but that's only with a apart screen, to Save as... And I've wrote a program to save it without the Save as... screen, but to the same path as the path from where I opened it, and saved it earlier.

    This is the code:
    Public Class Form1

    Dim Saved As Boolean = False, NewFile As Boolean = True, path As String

    Private Sub Button_Save_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button_Save.Click
    Save_File()
    End Sub

    Private Sub Button_Exit_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button_Exit.Click
    If Saved = False Then
    If MsgBox("Do you want to save the file before closing ", MsgBoxStyle.YesNoCancel) = MsgBoxResult.Yes Then
    Save_File()
    End
    ElseIf MsgBoxResult.No Then
    End
    Else
    'Do nothing
    End If
    Else
    End
    End If
    End Sub

    Private Sub Button_Open_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button_Open.Click
    Dim Open As New OpenFileDialog()
    Dim myStreamReader As System.IO.StreamReader
    Open.Filter = "Plain Text Files (*.txt)|*.txt|All files (*.*)|*.*"
    Open.CheckFileExists = True
    Open.Title = "Open"
    Open.ShowDialog(Me)
    Try
    Open.OpenFile()
    myStreamReader = System.IO.File.OpenText(Open.FileName)
    Text_Place.Text = myStreamReader.ReadToEnd()
    path = Open.FileName
    NewFile = False
    Saved = True
    'Here must be the setting of the path to the selected path
    Catch ex As Exception
    'Do nothing on Exception
    End Try
    End Sub

    Sub Save_File()
    Dim Save As New SaveFileDialog()
    Dim MyStreamWriter As System.IO.StreamWriter
    If NewFile = True Then
    Save.Filter = "Plain Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    Save.CheckPathExists = True
    Save.Title = "Save"
    Save.ShowDialog(Me)
    Try
    MyStreamWriter = System.IO.File.AppendText(Save.FileName)
    MyStreamWriter.Write(Text_Place.Text)
    MyStreamWriter.Flush()
    Saved = True
    NewFile = False
    'Here must be the setting of the path to the selected path
    Catch ex As Exception
    'Do nothing
    End Try
    Else
    'This is the place where the code for saving to the path for the already saved file
    End If
    End Sub

    Private Sub Text_Place_TextChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) _
    Handles Text_Place.TextChanged
    Saved = False
    End Sub

    End Class

    See the green rules, how do I declare "path" as the path of the saved or opened file And how do I save the file to that path

  • Other way of saving