|
-
Jul 2nd, 2011, 12:10 PM
#1
Thread Starter
Member
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
-
Jul 2nd, 2011, 12:16 PM
#2
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
-
Jul 2nd, 2011, 12:29 PM
#3
Thread Starter
Member
Re: How to save text in a textbox?
-
Jul 2nd, 2011, 12:30 PM
#4
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
-
Jul 2nd, 2011, 12:44 PM
#5
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/
-
Jul 2nd, 2011, 12:53 PM
#6
Thread Starter
Member
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:
My.Settings.Save_Textbox1 = TextBox1.Text My.Settings.Save() 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.
-
Jul 2nd, 2011, 01:02 PM
#7
Thread Starter
Member
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.
-
Jul 2nd, 2011, 01:09 PM
#8
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.
-
Jul 2nd, 2011, 01:18 PM
#9
Thread Starter
Member
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:
Return CType(Me("TextBoxValues"), Global.System.Collections.Specialized.StringCollection)
-
Jul 2nd, 2011, 01:36 PM
#10
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?
-
Jul 2nd, 2011, 02:08 PM
#11
Thread Starter
Member
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...?
-
Jul 2nd, 2011, 02:15 PM
#12
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.
-
Jul 2nd, 2011, 02:30 PM
#13
Thread Starter
Member
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?
-
Jul 2nd, 2011, 02:34 PM
#14
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"?
-
Jul 2nd, 2011, 02:37 PM
#15
Thread Starter
Member
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
-
Jul 2nd, 2011, 02:56 PM
#16
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).
-
Jul 2nd, 2011, 02:57 PM
#17
Re: How to save text in a textbox?
-
Jul 2nd, 2011, 03:07 PM
#18
Thread Starter
Member
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?
-
Jul 2nd, 2011, 03:19 PM
#19
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.
-
Jul 2nd, 2011, 03:20 PM
#20
Re: How to save text in a textbox?
Can you zip the test project and attach it here?
-
Jul 2nd, 2011, 03:24 PM
#21
Thread Starter
Member
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:
Imports System.IO Public Class Form1 Private Sub Label4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) End Sub Private Sub OpenProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenProjectToolStripMenuItem.Click OpenFileDialog1.ShowDialog() Dim objreader As New System.IO.StreamReader(TextBox1.Text) objreader.Close() End Sub Private Sub FileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FileToolStripMenuItem.Click End Sub Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk End Sub Private Sub SaveProjectToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProjectToolStripMenuItem.Click SaveFileDialog1.ShowDialog() System.IO.File.WriteAllText(SaveFileDialog1.FileName, TextBox1.Text) End Sub Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click End Sub Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Form2.Show() End Sub End Class
-
Jul 2nd, 2011, 03:37 PM
#22
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.
-
Jul 2nd, 2011, 03:48 PM
#23
Thread Starter
Member
Re: How to save text in a textbox?
-
Jul 2nd, 2011, 03:57 PM
#24
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.
-
Jul 2nd, 2011, 04:02 PM
#25
Thread Starter
Member
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?
-
Jul 2nd, 2011, 04:15 PM
#26
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.
-
Jul 2nd, 2011, 04:36 PM
#27
Thread Starter
Member
Re: How to save text in a textbox?
Can I PM you my "corrupted" project so you can see whats wrong with it?
-
Jul 2nd, 2011, 04:45 PM
#28
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|