|
-
Aug 16th, 2006, 05:42 PM
#1
Thread Starter
Member
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:
Dim fname As String
fname = ""
SaveFileDialog1.Filter = "Rich text documents |*.rtf"
fname = SaveFileDialog1.ShowDialog()
Dim text As String
text = RichTextBox1.Text
FileOpen(1, fname, OpenMode.Append, OpenAccess.Write, OpenShare.Shared)
Print(1, Me.RichTextBox1.Text)
FileClose(1)
I guess I am doing something totally wrong.
-
Aug 16th, 2006, 05:52 PM
#2
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:
If Me.SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.RichTextBox1.SaveFile(Me.SaveFileDialog1.FileName)
End If
Note that that will save in RTF format. SaveFile is overloaded if you want to save to a different format.
-
Aug 16th, 2006, 06:03 PM
#3
Thread Starter
Member
Re: Problem with saving a text file...
Hey thanks! I started with VB today...
-
Aug 16th, 2006, 06:06 PM
#4
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.
-
Oct 9th, 2006, 09:24 AM
#5
-
Oct 9th, 2006, 11:49 AM
#6
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...
-
Oct 9th, 2006, 05:45 PM
#7
Re: Problem with saving a text file...
 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.
-
Nov 9th, 2006, 08:44 PM
#8
Lively Member
Re: Problem with saving a text file...
oh ok then thanks a lot, It works now
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|