|
-
May 3rd, 2008, 04:52 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Open a textfile to richtextbox1
I want the notes.txt to be opened into richtextbox1 on form1_load, but can't figure out how...
I've tried:
Code:
If File.Exists("notes.txt") Then
RichTextBox1.LoadFile("notes.txt")
End If
And:
Code:
If File.Exists("notes.txt") Then
File.OpenRead("notes.txt")
End If
But none works :S
-
May 3rd, 2008, 04:55 AM
#2
Re: Open a textfile to richtextbox1
The first one is the one you want, except that you are probably not specifying the correct file. It's generally a bad idea to specify just a file name. You should almost universally specify the full path of the file. As it is that code will look for that file in the current working directory. That will generally be the program folder by default but not always and it can change. If you want to look in the program folder then you should do so using Application.StartupPath. If you want to look elsewhere then you should specify the exact path.
Last edited by jmcilhinney; May 3rd, 2008 at 05:05 AM.
-
May 3rd, 2008, 05:00 AM
#3
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
Ahh, now i found the error, but i need help solving it..
As i'm debugging i have the text file in the debug folder...
My program thinks i'm looking for debugnotes.txt
I may need to put "\" before notes.txt, but i don't exactly know how i can do it...
-
May 3rd, 2008, 05:04 AM
#4
Frenzied Member
Re: Open a textfile to richtextbox1
application.startuppath & "\notes.txt"
-
May 3rd, 2008, 05:08 AM
#5
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
Ahh, when i am going to learn that we use & instead of + in VB :S
But anyways i still get an error..
Code:
Code:
RichTextBox1.LoadFile(Application.StartupPath & "\notes.rtf")
Error:
Code:
The fileformat was invalid
-
May 3rd, 2008, 05:12 AM
#6
Frenzied Member
Re: Open a textfile to richtextbox1
-
May 3rd, 2008, 05:13 AM
#7
Re: Open a textfile to richtextbox1
vb.net Code:
IO.Path.Combine(Application.StartupPath, "notes.txt")
-
May 3rd, 2008, 05:22 AM
#8
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
Ahh, i don't understand what wrong, all my code looks perfect...
vb Code:
Imports System.IO
Public Class Form3
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
File.WriteAllText("notes.txt", RichTextBox1.Text)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If File.Exists("notes.txt") Then
Path.Combine(Application.StartupPath, "notes.txt")
End If
End Sub
End Class
But it won't save now as it always did before...
-
May 3rd, 2008, 05:27 AM
#9
Re: Open a textfile to richtextbox1
Tut, tut. Path.Combine is just to combine paths, not to load the file:
vb.net Code:
RichTextBox1.LoadFile(IO.Path.Combine(Application.StartupPath, "notes.txt"))
Also, you're using LoadFile to load the file so wouldn't you use SaveFile to save it?
-
May 3rd, 2008, 05:28 AM
#10
Frenzied Member
Re: Open a textfile to richtextbox1
 Originally Posted by kake_fisk
Ahh, i don't understand what wrong, all my code looks perfect...
vb Code:
Imports System.IO
Public Class Form3
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
File.WriteAllText("notes.txt", RichTextBox1.Text)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If File.Exists("notes.txt") Then
Path.Combine(Application.StartupPath, "notes.txt")
End If
End Sub
End Class
But it won't save now as it always did before...
The first sub is writing to a text file but the second is populating nothing
-
May 3rd, 2008, 05:38 AM
#11
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
***?
Code:
File Missing C:\notes.txt.
I used path.combine...
Code:
RichTextBox1.LoadFile(Path.Combine(Application.StartupPath, "\notes.txt"))
I tought this was a simple thing first :S
-
May 3rd, 2008, 05:48 AM
#12
Frenzied Member
Re: Open a textfile to richtextbox1
Go to the notes.txt, right click and select properties.
Copy and Paste the address for us to see or paste it in Start > Run and see what happins
-
May 3rd, 2008, 05:56 AM
#13
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
When i paste it in run it opens the textfile...
But i have another solution that maybe can fix the problem, what if i save it to specialfolder.mydocuments?
Do you know how i do that?
-
May 3rd, 2008, 05:59 AM
#14
Frenzied Member
Re: Open a textfile to richtextbox1
Check this first
Select the file in the solution explorer and check for
-
May 3rd, 2008, 06:17 AM
#15
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
should i upload it to the solution exlorer?
But what about saving it to my documents instead?
-
May 3rd, 2008, 07:03 AM
#16
Re: Open a textfile to richtextbox1
This is all getting a bit ridiculous. Please tell us exactly where this file is in the first place. If you haven't added it to your project, i.e. it doesn't appear in the Solution Explorer, then of course it's not going to be in the bin\Debug folder. That folder is cleared every time you build so if the file isn't copied to the output folder from the source folder then it won't exist. Is this file part of your project or not? If it is then make it part of your project. If it's not then the program folder is the wrong place for it anyway. If you want it in the My Documents folder then you'd just use My.Computer.FileSystem.SpecialDirectories.MyDocuments instead of Application.StartupPath. If this is specifically data for this app rather than a document, but the program folder is not appropriate to store it, then you can use My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData or .AllUsersApplicationData instead.
-
May 3rd, 2008, 08:54 AM
#17
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
Okay, thanks, i think i found a way now, but i'll post if it didn't work...
-
May 3rd, 2008, 09:17 AM
#18
Thread Starter
Addicted Member
Re: Open a textfile to richtextbox1
OOkay, this is very srange...
I use this code:
Code:
Imports System.IO
Public Class Form3
Dim notes As String = _
My.Computer.FileSystem.SpecialDirectories.MyDocuments _
& "\notes.txt"
Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
My.Computer.FileSystem.WriteAllText(notes, RichTextBox1.Text, False)
End Sub
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If My.Computer.FileSystem.FileExists(notes) Then
RichTextBox1.LoadFile(notes)
End If
End Sub
End Class
Then i get error about fileformat not invalid...
But wait a sec, in richtextbox, do i need to use .rtf instead of .txt?
-
May 3rd, 2008, 11:16 AM
#19
Re: [RESOLVED] Open a textfile to richtextbox1
Always pay attention to Intellisense. It will always tell you if a member is overloaded and if it is then you can always scroll through the list to see if there's a better option.
vb.net Code:
Imports System.IO Public Class Form1 Private filePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _ "notes.txt") Private Sub Form1_Load(ByVal sender As Object, _ ByVal e As EventArgs) Handles MyBase.Load If File.Exists(Me.filePath) Then Me.RichTextBox1.LoadFile(Me.filePath, _ RichTextBoxStreamType.PlainText) End If End Sub Private Sub Form1_FormClosing(ByVal sender As Object, _ ByVal e As FormClosingEventArgs) Handles Me.FormClosing Me.RichTextBox1.SaveFile(Me.filePath, _ RichTextBoxStreamType.PlainText) End Sub End Class
-
May 3rd, 2008, 12:14 PM
#20
Thread Starter
Addicted Member
Re: [RESOLVED] Open a textfile to richtextbox1
Yeah, i found it out, i marked the thread as solved 
I needed to put plaintext behind...
-
May 3rd, 2008, 12:19 PM
#21
Re: [RESOLVED] Open a textfile to richtextbox1
It's a good idea to post back to tell everyone what the solution was if you end up coming up with it yourself. That way we all know that you have the best solution you can and it also helps others in future who might have the same problem.
-
May 3rd, 2008, 12:23 PM
#22
Thread Starter
Addicted Member
Re: [RESOLVED] Open a textfile to richtextbox1
Okay, thanks for the tip
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
|