Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Is it possible to save the text typed into a text box after shutting the program down without writing it to a file?

Posted on 2007-11-28
11
321 Views
Last Modified: 2010-04-23
I have a textbox and a button.  I'd like to use the button to "save" the textbox.text until it is changed.  The textbox.text is  an e-mail address or one line.  Right now I type the  e-mail address into the textbox hit save button  and I make the textbox.visible = false and the button.visible = false.  The program runs and does what it is suppose to.  I shut the program down and next time I run it the textbox is cleared.  Is there an easier way to save one line in a textbox without writing to a file and reading back every time the program runs?
0
Comment
Question by:markleszczynski
11 Comments
 
LVL 7

Expert Comment

by:sj_hicks
ID: 20370974
In VS.NET 2005, you can use My.Settings which allows settings to be saved and loaded.  I think it actually saves an XML file in user profiles, but this is all handled by the .NET framework and you don't need to manage the file directly from the code.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20370990
You can save into a registry key.
The settings option, saves to a file into user's appdata directory.
0
 

Author Comment

by:markleszczynski
ID: 20371297
I am trying to save the text entered into the txtaddress text box when I close the application.  I tried to use My.settings in the txtaddress_TextChanged event.  It doesn't like that.  Not sure if that is even the right event to be saving the text.  The code I used is below, doesn't work.  Not sure what I'm doing wrong.


   
  Private Sub txtaddress_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtaddress.TextChanged
        My.Settings.Save(txtaddress)
 
 
 
    End Sub

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 19

Expert Comment

by:arif_eqbal
ID: 20372622
A simpler way to do the same:
Go to Design View
Open the Property Pages of the TextBox
If you have sorted the property view alphabetically the first Prperty will be "ApplicationSettings"
Expand it Click on thre "..." against "PropertyBindings"
From the Dialog that opens select the "Text" property in the Drop Down Click "New"
Give a Name to your setting say "EMail" here
Thats it.....
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 20372654
And BTW if you want to do it in Code as you were trying earlier
Look at the code snippet
I have used the "Leave" event rather than "TextChanged" if it is important for your app you can go for TextChanged event as well

 Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
        My.Settings.Item("EMail") = TextBox1.Text
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = My.Settings.Item("EMail")
    End Sub

Open in new window

0
 

Author Comment

by:markleszczynski
ID: 20378805
The above code does not work it crashes the program with a "settings property e-mail was not found"
I am trying to save the last text  that was typed into txtAddress.text when then program closes, a variable.  I also tried the "simpler way"  in Design Mode.  That lets me enter a default value.  I need a variable.  Do I need to declare EMail as a variable?  the only changes i made to my application are the two lines in the code snippet regarding My.Settings.
 Private Sub txtaddress_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtaddress.Leave
        My.Settings.Item("EMail") = txtaddress.Text
 
    End Sub
 
 Private Sub Equipment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnCloseEquipment.BackColor = Color.DarkSalmon
        Me.BackColor = Color.LightCyan
        txtaddress.Visible = False
        btnSave.Visible = False
        txtaddress.Text = My.Settings.Item("EMail")
 
    End Sub

Open in new window

0
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 250 total points
ID: 20380129
I am sorry, you shoudl be doing this Error Checking, I forgot to add that.
Why this happens is, when you run it for the first time, you do not have the Property Setting by the name "EMail" and you are trying to use it, so the Error
All you need is check if this "EMail" setting is available or not

If My.Settings.Properties.Item("EMail") IsNot Nothing Then
txtaddress.Text = My.Settings.Item("EMail")
End If
0
 

Author Comment

by:markleszczynski
ID: 20380138

This code would be in the form load event?
If My.Settings.Properties.Item("EMail") IsNot Nothing Then
txtaddress.Text = My.Settings.Item("EMail")
End If
0
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 20380174
Yes
0
 

Author Comment

by:markleszczynski
ID: 20386682
I'm getting closer.  I added the error checking code as advised above.  It no longer crashes the program but the text still is not saved.  Whatever text is in txtaddress is still not saved when I shut down the program.  The next time I start the program txtaddres.text is blank.  I attached the code I am using to "write" txtaddress.text and "read back" txtaddress.text.  

write
 
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        txtaddress.Visible = False
        btnSave.Visible = False
        My.Settings.Item("EMail") = txtaddress.Text
        My.Settings.Save()
 
Read back
 
 Private Sub Equipment_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnCloseEquipment.BackColor = Color.DarkSalmon
        Me.BackColor = Color.LightCyan
        txtaddress.Visible = False
        btnSave.Visible = False
        If My.Settings.Properties.Item("EMail") IsNot Nothing Then
            txtaddress.Text = My.Settings.Item("EMail")
        End If

Open in new window

0
 

Author Comment

by:markleszczynski
ID: 20386758
Thanks for all the help.  It is finally working.  I had the Email property set to application it was read - only.

Soon as I set it to User it becam read/write.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 Populate DataModel with DataTable 44 38
write xml in vb.net 2 32
Convert datetime to time string 10 28
VB.Net. Reading xml value 6 29
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question