Results 1 to 5 of 5

Thread: [RESOLVED] Saving Contents of a Tich Text box to a .txt file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    28

    Resolved [RESOLVED] Saving Contents of a Tich Text box to a .txt file

    Hey,

    First Time here and fairly new to vb.(studying at college)

    I have a Richtext box and a save button on a form and i was wondering how to save it to my documents, as note.txt or something? ive been on msdn site and i cant seem to get there's working and i've used the ones in the help file in visual studios 2008 but its a c# code so i cant get that working either.

    and Also how do i make programs i have made into stand alone apps? i do have images in it aswell?

    Thanks Alot and help is much appreciated!


  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Saving Contents of a Tich Text box to a .txt file

    You can use the SaveFile method of the RichTextBox class... like so:
    vb Code:
    1. RichTextBox1.SaveFile("C:\test.txt")

    Let me know if you need a better explanation but I'm guessing you can figure out what you need to change to suit your needs.

    Oh yeah and to get the My Documents folder location you can just use this:

    vb Code:
    1. My.Computer.FileSystem.SpecialDirectories.MyDocuments

    So combining those two things would end up with this (which should do exactly what you want)

    vb Code:
    1. Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    2.   RichTextBox1.SaveFile(mydoclocation & "\test.txt", RichTextBoxStreamType.PlainText)
    I've used the PlainText bit as the second argument for the SaveFile method because otherwise you end up with a load of rubbish in your text file along with the text in the RichTextBox
    Last edited by chris128; Jul 25th, 2008 at 06:27 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    28

    Re: Saving Contents of a Tich Text box to a .txt file

    thankyou for the fast reply i got that working and it was exactly what i needed thankyou,

    If i wanted this to appear in the the rtb when the program is restarted how would i go about this?

  4. #4
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Saving Contents of a Tich Text box to a .txt file

    You would use the LoadFile method of the RichTextBox in the same way.

    So in your Form Load event you would put something like this:

    vb Code:
    1. Dim filelocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
    2. RichTextBox1.LoadFile(filelocation & "\test.txt", RichTextBoxStreamType.PlainText)

    Its always a good idea to look through the properties of a class first before searching/asking how to do something. You can do this by just typing the name of the control for example, RichTextBox1 followed by a full stop and then intellisense will bring up a list of all the properties and methods of that control

    If you are going to be using the same location string twice in your app (once for the loading and once for the saving) then you might want to declare it at class level instead of declaring it twice - once in each event. However, its not really going to make any difference performance wise in this case so its up to you.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2008
    Posts
    28

    Re: Saving Contents of a Tich Text box to a .txt file

    Thankyou ill bare in mind to look through the properties in future, its all so new learning vb.net, php, at the same time because you would know how to do it in one language but not the other.

    Thankyou Once again and it is much appreciated!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width