Results 1 to 22 of 22

Thread: [RESOLVED] Open a textfile to richtextbox1

  1. #1

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    Resolved [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

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

    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.
    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
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    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...

  4. #4
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Open a textfile to richtextbox1

    application.startuppath & "\notes.txt"

  5. #5

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    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

  6. #6
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Open a textfile to richtextbox1

    rtf or txt?

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

    Re: Open a textfile to richtextbox1

    vb.net Code:
    1. IO.Path.Combine(Application.StartupPath, "notes.txt")
    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

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    Re: Open a textfile to richtextbox1

    Ahh, i don't understand what wrong, all my code looks perfect...
    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form3
    4.  
    5.     Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    6.         File.WriteAllText("notes.txt", RichTextBox1.Text)
    7.     End Sub
    8.  
    9.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         If File.Exists("notes.txt") Then
    11.             Path.Combine(Application.StartupPath, "notes.txt")
    12.         End If
    13.     End Sub
    14. End Class
    But it won't save now as it always did before...

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

    Re: Open a textfile to richtextbox1

    Tut, tut. Path.Combine is just to combine paths, not to load the file:
    vb.net Code:
    1. 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?
    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

  10. #10
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Open a textfile to richtextbox1

    Quote Originally Posted by kake_fisk
    Ahh, i don't understand what wrong, all my code looks perfect...
    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form3
    4.  
    5.     Private Sub Form3_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    6.         File.WriteAllText("notes.txt", RichTextBox1.Text)
    7.     End Sub
    8.  
    9.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    10.         If File.Exists("notes.txt") Then
    11.             Path.Combine(Application.StartupPath, "notes.txt")
    12.         End If
    13.     End Sub
    14. 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

  11. #11

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    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

  12. #12
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  13. #13

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    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?

  14. #14
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Open a textfile to richtextbox1

    Check this first

    Select the file in the solution explorer and check for


  15. #15

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    Re: Open a textfile to richtextbox1

    should i upload it to the solution exlorer?

    But what about saving it to my documents instead?

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

    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.
    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

  17. #17

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    Re: Open a textfile to richtextbox1

    Okay, thanks, i think i found a way now, but i'll post if it didn't work...

  18. #18

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    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?

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

    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:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.     Private filePath As String = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, _
    6.                                               "notes.txt")
    7.  
    8.     Private Sub Form1_Load(ByVal sender As Object, _
    9.                            ByVal e As EventArgs) Handles MyBase.Load
    10.         If File.Exists(Me.filePath) Then
    11.             Me.RichTextBox1.LoadFile(Me.filePath, _
    12.                                      RichTextBoxStreamType.PlainText)
    13.         End If
    14.     End Sub
    15.  
    16.     Private Sub Form1_FormClosing(ByVal sender As Object, _
    17.                                   ByVal e As FormClosingEventArgs) Handles Me.FormClosing
    18.         Me.RichTextBox1.SaveFile(Me.filePath, _
    19.                                  RichTextBoxStreamType.PlainText)
    20.     End Sub
    21.  
    22. End Class
    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

  20. #20

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    Re: [RESOLVED] Open a textfile to richtextbox1

    Yeah, i found it out, i marked the thread as solved
    I needed to put plaintext behind...

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

    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.
    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

  22. #22

    Thread Starter
    Addicted Member kake_fisk's Avatar
    Join Date
    May 2008
    Posts
    207

    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
  •  



Click Here to Expand Forum to Full Width