Results 1 to 8 of 8

Thread: [RESOLVED] Add text to txt file from texbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Resolved [RESOLVED] Add text to txt file from texbox

    I already know how add text but my problem is add a new line without remove the others lines. for example
    textbox1="text1"
    When I press a button I want to add "text1" into the txt file

    textbox1="text2"
    When I press a button I want to add "text2" and keep "Text1" into the txt file:
    text1
    text2



    Can you help me?

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

    Re: Add text to txt file from texbox

    You have two choices. Under the hood they will do pretty much exactly the same thing but the code you write will change a little.

    First, you can create a StreamWiter. The constructor provides an option to specify whether to overwrite existing data or append. You can then call WriteLine and/or Write, depending on whether you want the file to end with a line break or you want to write the line break before each new bit of text.

    Second, you can call File.AppendAllText. In that case you would provide a String to write that ended with a line break, which you would add using Environment.NewLine.
    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
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Re: Add text to txt file from texbox

    Ok I have this code
    Dim W As New IO.StreamWriter("C:\created.txt")
    W.Write(TextBox1.Text)
    W.Close()

    But this code overwrite son I need append the txt but I don't know how.

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

    Re: Add text to txt file from texbox

    As I said, the StreamWriter constructor provides the option to specify whether to overwrite or append. The logical thing to do is to consult the MSDN documentation for the StreamWriter constructor to see what that option is and how to use it. Whenever you have the name of a type and/or member and you need information about it, the first thing you do should be to read the relevant documentation.
    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
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Add text to txt file from texbox

    There's also the appending boolean value for a secondary parameter in StreamWriter

    Code:
    StreamWriter("File.txt", True)
    True would tell it that append is acceptable as a boolean value (True) of course. Non tested, but from what I can remember it's as easy as that.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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

    Re: Add text to txt file from texbox

    Quote Originally Posted by AceInfinity View Post
    There's also the appending boolean value for a secondary parameter in StreamWriter
    It's not "also" because that's exactly what I was talking about. Now that you have deprived the OP of the need to consult the documentation, the next time a similar issue arises, they will likely have to post again rather than being able to consult the documentation and solve the issue for themselves. @romanos8, hopefully you will still take a look at the documentation so you can see how easy it can be to solve a lot of your own problems.
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jul 2011
    Posts
    294

    Re: Add text to txt file from texbox

    Thanks I already fix the problem.

  8. #8
    Fanatic Member AceInfinity's Avatar
    Join Date
    May 2011
    Posts
    696

    Re: Add text to txt file from texbox

    Quote Originally Posted by jmcilhinney View Post
    It's not "also" because that's exactly what I was talking about. Now that you have deprived the OP of the need to consult the documentation, the next time a similar issue arises, they will likely have to post again rather than being able to consult the documentation and solve the issue for themselves. @romanos8, hopefully you will still take a look at the documentation so you can see how easy it can be to solve a lot of your own problems.
    I didn't realize it was a game to get him to go read the documentation. Although good for him to read documentation just like any other information available in a standard documentation, it does leave this forum with less activity, until there's people that don't understand documentation, and have to resort to further alternatives. I was only trying to help, so thanks for seeing that side of the equation.
    <<<------------
    Improving Managed Code Performance | .NET Application Performance
    < Please if this helped you out. Any kind of thanks is gladly appreciated >


    .NET Programming (2012 - 2018)
    ®Crestron - DMC-T Certified Programmer | Software Developer
    <<<------------

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