Results 1 to 28 of 28

Thread: How to save text in a textbox?

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    How to save text in a textbox?

    How do I save the text in a textbox?

    I want my application to save the text my users leaves in (the text includes details for the accounts)

    Im not sure how to do it where they save it to a text document or maybe if there is a way to save a cookie on the textbox so when the close my app and open it again there infos still there

  2. #2
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: How to save text in a textbox?

    save it to settings.

    First add a setting in the project properties window, declare its name and type.

    then do something like My.Settings.TextToSave = TextBox1.Text

    and when you open your app do it the other way around TextBox1.Text = My.Settings.TextToSave
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    How do I add a setting?

  4. #4
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    Re: How to save text in a textbox?

    as I said, look in the project properties, the settings tab. just type the name and select the type on the list. Project properties are accesible from the project menu.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  5. #5
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    Have a look at this article, which shows one easy way using the My.Settings class:
    http://pradeep1210.wordpress.com/201...-on-your-form/
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  6. #6

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Its not working for me...

    I set up everything in the Project Settings, then in a button (which I called Save) I put the code:

    vb Code:
    1. My.Settings.Save_Textbox1 = TextBox1.Text
    2.  
    3.         My.Settings.Save()
    4.  
    5.         TextBox1.Text = My.Settings.Save_Textbox1

    No errors showed up so I assumed it would work, so I opened my program in debug mode and even went to the direct excecutable and I added text to Textbox1 i clicked the button called save, then i closed my program and reopened it to see if it was still there and it wasnt.

  7. #7

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Pradeep I have two errors that say:

    Error 1 'textBoxes' is not declared. It may be inaccessible due to its protection level.
    Error 2 'textBoxes' is not declared. It may be inaccessible due to its protection level.

  8. #8
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    You forgot to copy this line:
    Code:
    Dim textBoxes() As TextBox
    Put it somewhere near the top of your form (in the form declaration section), outside any sub/function.
    Then try again.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  9. #9

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Now when I run my program put text in the textboxes and click save it redirects to Settings.Designer.vb and highlights this piece

    vb Code:
    1. Return CType(Me("TextBoxValues"), Global.System.Collections.Specialized.StringCollection)

  10. #10
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    Where is Save button?
    What code do you have there?
    What is the error message shown?

    Since the sample code I had given doesn't have a save button, I would assume that you have moved the code of the FormClosing event into your Save button click. Is it so?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  11. #11

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    I saved all the code in the save button. Should I save it outside the sub or...?

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    For easiness,
    1. Remove whatever changes you made, so that your form code looks as it was before this change.
    2. Create the setting named TextBoxValues, as described there, if it is not already present.
    3. Copy the code from that link and paste inside the Form (below the line "Public Class Form1"), outside any sub/function.
    4. Run the application. It should work as expected.
    Last edited by Pradeep1210; Jul 2nd, 2011 at 02:19 PM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Its not even letting me run it this time, it goes back and highlights the code. I tested it out and removed the code that I copied and it ran perfectly. Do you know whats wrong?

  14. #14
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    What is the error message shown when it highlights that line?

    Also can you double check that the scope of that TextBoxValues setting you created is set to "User"?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  15. #15

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    The error highlighted is:

    Return CType(Me("TextBoxValues"), Global.System.Collections.Specialized.StringCollection)

    and it is set to user

  16. #16
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    There seems something bad with your project.

    Try this for a test:
    1. Create a new Windows Forms project.
    2. Put 4 textboxes on the form (the default form Form1)
    3. Create a Setting named "TextBoxValues", Type: System.Collections.Specialized.StringCollection, Scope: User
    4. Copy the code from that link and paste it in the Form code.
    5. Run the application and see what happens.

    If it works, then there is surely something bad with your application and you will have to fix it in a different way (not related to this thread).
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: How to save text in a textbox?

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  18. #18

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    @Pradeep Nope I still get the same error. Maybe there might be something wrong with the code you provided, no offense?

  19. #19
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    It could be possible that there's something wrong with the code.

    I had tested it with .NET 3.5 (visual studio 2008) before posting it, and its unlikely that it won't work with VS 2010.

    At present, I'm not on a system where development environment is installed and I can test that code.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  20. #20
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    Can you zip the test project and attach it here?
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  21. #21

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Can I post my raw code instead? Cause I am working on a project (I was the one who PM'ed you about httpwebrequest) and I do not want the world to know bout it...yet.

    vb Code:
    1. Imports System.IO
    2.  
    3. Public Class Form1
    4.  
    5.  
    6.     Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    7.  
    8.     End Sub
    9.  
    10.  
    11.     Private Sub OpenProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProjectToolStripMenuItem.Click
    12.         OpenFileDialog1.ShowDialog()
    13.         Dim objreader As New System.IO.StreamReader(TextBox1.Text)
    14.         objreader.Close()
    15.  
    16.     End Sub
    17.  
    18.     Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click
    19.  
    20.     End Sub
    21.  
    22.     Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
    23.  
    24.     End Sub
    25.  
    26.     Private Sub SaveProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProjectToolStripMenuItem.Click
    27.         SaveFileDialog1.ShowDialog()
    28.         System.IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text)
    29.     End Sub
    30.  
    31.     Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click
    32.  
    33.     End Sub
    34.  
    35.    
    36.  
    37.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    38.  
    39.     End Sub
    40.  
    41.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    42.         Form2.Show()
    43.     End Sub
    44. End Class

  22. #22
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    The problem looks like more in the Settings file rather than the Form. So this doesn't help.

    BTW I was talking about attaching the new test project you created (post #16) and not your original project.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  23. #23

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Ok heres the download link: http://www.mediafire.com/?uqrldnno788vt6r

  24. #24
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    That zip doesn't contain any setting file. It has just Form2.* files.

    Looks like you tried with your existing project, which we assumed is corrupt. So that would be of no use.

    Try with a new project as suggested in post #16. Then if it is not working, zip the entire project and attach it here.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  25. #25

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    It worked in a new project. Hmm do you know what migth be wrong with my old one?

  26. #26
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to save text in a textbox?

    ok.. So there is nothing wrong with the code or method I suggested.

    It means that something is bad about your project. I would suspect the settings file.

    You can try investigating into it. But the easiest thing I can think of is:
    1. Create a new Project.
    2. Add your existing forms to that project. They will automatically be copied there.
    3. Run that project.
    4. If everything is fine, backup/remove your existing project and use the new one from now on.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  27. #27

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Can I PM you my "corrupted" project so you can see whats wrong with it?

  28. #28

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    54

    Re: How to save text in a textbox?

    Nevermind, when I opened my old project Visual Basic said it will fix an error in it and it did. so when i implemented the code it worked.

    Thanks soooo much

Tags for this Thread

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