Results 1 to 8 of 8

Thread: Problem with saving a text file...

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    51

    Problem with saving a text file...

    Heres the story...
    I am making a notepad thing, and I have come to saving the text into a text file.
    It works... Sort of. I can get up the SaveDialog, but whatever I type, the saved filename will just be "1". However, I renamed the file, and the text was written fine. But something is wrong with saving the file.

    I dont know...
    Here is the save code:
    VB Code:
    1. Dim fname As String
    2.         fname = ""
    3.         SaveFileDialog1.Filter = "Rich text documents |*.rtf"
    4.         fname = SaveFileDialog1.ShowDialog()
    5.  
    6.         Dim text As String
    7.         text = RichTextBox1.Text
    8.         FileOpen(1, fname, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
    9.         Print(1, Me.RichTextBox1.Text)
    10.         FileClose(1)
    I guess I am doing something totally wrong.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with saving a text file...

    Firstly, the ShowDialog method of the SaveFileDialog doesn't return a file name. Like every ShowDialog method it retruns a DialogResult value. You get the selected file name from the FileName property of the SaveFileDialog.

    Secondly, don't use that old VB6 style of I/O. Assuming that you're using VB 2005 (please specify in future) you should use the members of the System.IO namespace, including the StreamReader and File classes, which can both save text files, or members of the My.Computer.FileSystem object.

    Thirdly, in this case you don't even need that. The RichTextBox has its own method.
    VB Code:
    1. If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
    2.     Me.RichTextBox1.SaveFile(Me.SaveFileDialog1.FileName)
    3. End If
    Note that that will save in RTF format. SaveFile is overloaded if you want to save to a different format.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2006
    Posts
    51

    Re: Problem with saving a text file...

    Hey thanks! I started with VB today...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with saving a text file...

    Rule number 1: take notice of what Intellisense tells you. It will give you descriptions of types, properties and methods along the way and tell you what things are for and what they can do. It can't tell you everything but it will tell you a lot.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Unhappy Re: Problem with saving a text file...

    hello, i've tried your method and i keep getting:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs17 Test\par
    }

    this all appears in the txt file
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

  6. #6
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: Problem with saving a text file...

    Because you opened it with NotePad instead of a RichText editor or viewer. That is what the RichText file actually contains, which are markup tags n all sorts of things. Open the file with WordPad, or Office, or some other RichText program in order to view it properly...

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with saving a text file...

    Quote Originally Posted by smart_phil_dude1
    hello, i've tried your method and i keep getting:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs17 Test\par
    }

    this all appears in the txt file
    As I said in my post, that code will save the file to RTF format, which, as Gig says, contains all the markup to format the text. It's kind of like HTML tags. If you open an HTML file in a straight text editor like Notepad you see all the HTML tags, but if you open it in a browser you see the page rendered as those tags instruct. If you open an RTF file in a straight text editor you'll see all the RTF markup, but if you open it in an RTF editor like WordPad then you'll see the page rendered as that markup instructs, with text colours, font, etc. If you want to save the contents of a RichTextBox as plain text then you have to do as I said in my previous post and use an overload of SaveFile to specify that format. As I also said previously, Intellisense can guide you in doing that.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Lively Member smart_phil_dude1's Avatar
    Join Date
    Jun 2005
    Location
    Behind You!
    Posts
    125

    Re: Problem with saving a text file...

    oh ok then thanks a lot, It works now
    Please Help Us Save Ana or donate now by going to Oneana.com

    Visit my Website at http://www.therealfantasy.com

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