By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,985 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

Save checkbox settings on close

P: 29
Hi

I have a program that sends an email to selected recipients.

I have two forms. One that creates and send the email and another that is used to select recipients and other information.

The program works great but everytime I close the program I lose all the settings for the recipients and other information.

The recipients are selected using checkboxes and other information for email is in text boxes.

Is there anyway to save these settings when the program is closed.
May 6 '07 #1
Share this Question
Share on Google+
17 Replies


Atran
100+
P: 319
First put to your Form, FormClosing Event Handler, or FormClosed Event Handler, or not do that now, then Check this Code:

My.Settings.Save()

I hope this is helpful.
May 6 '07 #2

P: 29
Hi Atran

I thought it was going to be that simple too.

I've tried including my.settings.save on form closing and also added my.settings.reload on form load but no joy.

Is there something else I have to do to make this setting work?
May 6 '07 #3

P: 29
Can anyone please help me with this.

Having to redo the settings every time I close the form is driving me #^%$

Any help appreciated
May 9 '07 #4

Atran
100+
P: 319
Hello Lazareth:
I will give you a simple idea.
example: I put in my Form Application a Textbox, then make The Textbox text to a notpad text in your windows.
Like if I have a notpad in my desktop, I can make the text of The Textbox take the notpad text, and when the Textbox text changed, that changing in the notpad...... (professionals use for this not notpad, they using DLL.....)

If you want the way, answer me to post the code.
Bye.
May 9 '07 #5

Atran
100+
P: 319
Hello Lazareth:
I will give you a simple idea.
example: I put in my Form Application a Checkbox, and create a notpad in your directory. Make you Checkbox true value see the notpad text, that you can make the checkbox check the notpad text, if the notpad text was the text the checkbox want the checkbox must be true, if the notpad text wasnt the text the checkbox want the checkbox must be false....
Godbye...............
May 9 '07 #6

Atran
100+
P: 319
If you want I can send you the code............................... Answer me.
May 9 '07 #7

P: 29
Hi Atran,

That will be great if you can post the code as this is driving me nuts and anything that will overcome this I will appreciate.

Will I need a notepad doc for every checkbox and textbox as their is about 18 checkboxes and 4 text boxes.

I suppose once I see the code I'll know and not a major as I will be happy just to overcome this.

Thanks for your help
May 10 '07 #8

Atran
100+
P: 319
Hi Atran,

That will be great if you can post the code as this is driving me nuts and anything that will overcome this I will appreciate.

Will I need a notepad doc for every checkbox and textbox as their is about 18 checkboxes and 4 text boxes.

I suppose once I see the code I'll know and not a major as I will be happy just to overcome this.

Thanks for your help
AS SOON, I will do!
May 10 '07 #9

TRScheel
Expert 100+
P: 638
Hi

I have a program that sends an email to selected recipients.

I have two forms. One that creates and send the email and another that is used to select recipients and other information.

The program works great but everytime I close the program I lose all the settings for the recipients and other information.

The recipients are selected using checkboxes and other information for email is in text boxes.

Is there anyway to save these settings when the program is closed.
XML or a database.

I would SUGGEST writing the change in information at the time it changes, although you could do it when the program closes, but if it closes erratically you could lose that information.

XML would be easiest.

Expand|Select|Wrap|Line Numbers
  1. Dim XMLDocument As System.Xml.XmlDocument = New System.Xml.XmlDocument()
  2.         Dim XMLParentElement As System.Xml.XmlElement = XMLDocument.CreateElement("CheckBoxes")
  3.         XMLDocument.AppendChild(XMLParentElement)
  4.  
  5.         'Foreach checkbox do this:
  6.         Dim XMLNode As System.Xml.XmlNode = XMLDocument.CreateNode(Xml.XmlNodeType.Element, Nothing, "TheCheckBox", Nothing)
  7.         Dim XMLAttribute As System.Xml.XmlAttribute = XMLDocument.CreateAttribute("Checked")
  8.         XMLAttribute.Value = "true"
  9.         XMLNode.Attributes.Append(XMLAttribute)
  10.         XMLParentElement.AppendChild(XMLNode)
  11.         'Till here, again, foreach check box
  12.  
  13.         Dim SWriter As System.IO.StreamWriter = New System.IO.StreamWriter("Mylocation.xml")
  14.         SWriter.Write(XMLDocument.InnerXml)
  15.         SWriter.Close()
There's the writing to the file, the reading is much the same, just backwards.

You would use a streamreader, and then readtoend, and pass that to XMLDocument.LoadXML
May 10 '07 #10

Atran
100+
P: 319
Hi Atran,

That will be great if you can post the code as this is driving me nuts and anything that will overcome this I will appreciate.

Will I need a notepad doc for every checkbox and textbox as their is about 18 checkboxes and 4 text boxes.

I suppose once I see the code I'll know and not a major as I will be happy just to overcome this.

Thanks for your help
Hello Again. I find the code but it is hard to explain it here, I put the code under, but you can enter my email, and download the project from attachement panel files, [email removed]
[message removed]

Bye.
------------------------------------------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1. Imports System.IO
  2. Public Class Form1
  3.  
  4.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  5.  
  6.  
  7.         'Let's Start form Here:
  8.         'Read carefully to understand the code, it is easy, but long.
  9.         'When you want to run this, open the Settings Savers Folder and Click the Button and see what is happen in the Folder.
  10.         'The problem is here some times you must click on button 3 or 4 times to do the action, do not think about this yet, first you need to understand the code.
  11.         'And The seconde problem is I put the files path here "/../../Settings Savers/", then the program contact this Folder when it is in "bin" Folder, if you want to fix that you must write in the files path "/Settings Savers/" but this will be error when you debug and run it from here, you must take the EXE Program and put it with the "Settings Savers" folder not inside the folder, with the folder.
  12.         ''''''''''''''''''''''''''''''''
  13.  
  14.         Try
  15.             If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then 'This mean "../../" in your project path, in "My Application/My Application/"
  16.                 CheckBox1.Checked = True
  17.             End If
  18.         Catch ex As Exception
  19.         End Try
  20.  
  21.         ''''''''''''''''''''''''''''''''
  22.  
  23.         Try
  24.             If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
  25.                 CheckBox1.Checked = False
  26.             End If
  27.         Catch ex As Exception
  28.         End Try
  29.  
  30.         '''''''''''''''''''''''''''''''''
  31.  
  32.         Try
  33.             If (My.Computer.FileSystem.FileExists("../../Settings Savers/TextBox1.txt")) Then
  34.                 TextBox1.Text = "File Found: " + My.Computer.FileSystem.ReadAllText("../../Settings Savers/TextBox1.txt")
  35.             End If
  36.         Catch ex As Exception
  37.         End Try
  38.  
  39.         '''''''''''''''''''''''''''''''''
  40.  
  41.         Try
  42.             If (My.Computer.FileSystem.FileExists("../../Settings Savers/TextBox1.txt").Equals(False)) Then
  43.                 TextBox1.Text = "Not Found!"
  44.             End If
  45.         Catch ex As Exception
  46.         End Try
  47.  
  48.         '''''''''''''''''''''''''''''''''
  49.  
  50.     End Sub
  51.  
  52.     Private Sub SaveSettingsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSettingsButton.Click
  53.  
  54.         Try
  55.  
  56.             If (CheckBox1.Checked = True) Then
  57.                 If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
  58.                     MessageBox.Show("True Check was Arealy saved")
  59.                 End If
  60.  
  61.                 If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
  62.                     System.IO.File.Create("../../Settings Savers/CheckBox1.txt")     'Create The CheckBox1.txt File
  63.                 End If
  64.             End If
  65.  
  66.             '''''''''''''''''''''''''''''''''
  67.  
  68.             If (CheckBox1.Checked = False) Then
  69.                 If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
  70.                     My.Computer.FileSystem.DeleteFile("../../Settings Savers/CheckBox1.txt")
  71.                 End If
  72.  
  73.                 If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
  74.                     MessageBox.Show("False Check was Saved!")
  75.                 End If
  76.             End If
  77.  
  78.         Catch ex As Exception
  79.             Return
  80.         End Try
  81.  
  82.     End Sub
  83.  
  84.     Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
  85.  
  86.         Try
  87.  
  88.             Dim Message As String = "Save your settings?"
  89.             Dim Caption As String = "Attention!"
  90.             Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
  91.             Dim Result As DialogResult
  92.             Result = MessageBox.Show(Message, Caption, Buttons)
  93.             If Result = DialogResult.Yes Then
  94.                 If (CheckBox1.Checked = True) Then
  95.                     If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
  96.                         MessageBox.Show("True Check was Arealy saved")
  97.                     End If
  98.  
  99.                     If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
  100.                         System.IO.File.Create("../../Settings Savers/CheckBox1.txt")     'Create The CheckBox1.txt File
  101.                     End If
  102.                 End If
  103.  
  104.                 '''''''''''''''''''''''''''''''''
  105.  
  106.                 If (CheckBox1.Checked = False) Then
  107.                     If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
  108.                         My.Computer.FileSystem.DeleteFile("../../Settings Savers/CheckBox1.txt")
  109.                     End If
  110.  
  111.                     If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
  112.                         MessageBox.Show("False Check was Saved!")
  113.                     End If
  114.                 End If
  115.             End If
  116.  
  117.         Catch ex As Exception
  118.  
  119.         End Try
  120.     End Sub
  121. End Class
May 11 '07 #11

Atran
100+
P: 319
Check this site:
http://www.thescripts.com/forum/thread643188.html
May 11 '07 #12

Atran
100+
P: 319
Hi Atran,

That will be great if you can post the code as this is driving me nuts and anything that will overcome this I will appreciate.

Will I need a notepad doc for every checkbox and textbox as their is about 18 checkboxes and 4 text boxes.

I suppose once I see the code I'll know and not a major as I will be happy just to overcome this.

Thanks for your help
Hello Again, Open this link to download the example, you will finish, and have the example, if any reason you cant download it till me.

Open this Link:

http://www.smokey-services.eu/forum/viewtopic.php?p=13731#13731


The file you will be download name is "Extract it before run it.zip"
Bye.
May 12 '07 #13

P: 29
Hello Again, Open this link to download the example, you will finish, and have the example, if any reason you cant download it till me.

Open this Link:

http://www.smokey-services.eu/forum/viewtopic.php?p=13731#13731


The file you will be download name is "Extract it before run it.zip"
Bye.
Hi Atran,

Thanks for all your help.

There is no download available at this link.

Do I need to register to this site before I can see it?
May 12 '07 #14

Atran
100+
P: 319
Tutorial, Step by Step.

Hi Atran,

Thanks for all your help.

There is no download available at this link.

Do I need to register to this site before I can see it?
Hi Lazareth,in That web site, I set the file, but after I see the web site says the file was failed. I have no way to send the project.

If the code was take time you do not read it. Then see this code below if you understand this, you will know to build that all:
-------------------------------------------------
FileExists means File Found or !!!
start the code:
Expand|Select|Wrap|Line Numbers
  1. If(My.Computer.FileSystem.FileExists("C:\test.txt")) Then
  2.      CheckBox1.Checked = True
  3. End If
  4.  
This code is checking the file "test.txt" if the file is in the "C:\"
If the file found, the checkbox will be true.
--------------------------------------------------
start this code:
Expand|Select|Wrap|Line Numbers
  1. If(My.Computer.FileSystem.FileExists("C:\test.txt").Equals(Flase)) Then
  2.      CheckBox1.Checked = Flase
  3. End If
  4.  
This code is checking the file "test.txt" if the file is not in the "C:\" because we write ".Equals(Flase)", the checkbox will be False.
---------------------------------------------------
If the File wasnt, this code will be shown error:
Expand|Select|Wrap|Line Numbers
  1. If(My.Computer.FileSystem.FileExists("C:\test.txt")) Then
  2.      CheckBox1.Checked = True
  3. End If
  4.  
Then you can fix that by making Try Command:
Expand|Select|Wrap|Line Numbers
  1. Try
  2.  
  3.      If(My.Computer.FileSystem.FileExists("C:\test.txt")) Then
  4.           CheckBox1.Checked = True
  5.      End If
  6.  
  7.      Catch ex As Exception
  8.  
  9. End Try
  10.  
Try command will Try, if the code was not found it way, it continue, it just try.
---------------------------------------------------
You see the path we add is "C:\test.txt" how you can to add it with the directory of your program debuged, you can write this "/test.txt", in visual project folders, the program will debuged in "/bin/Debug/MyProgram.exe" you can put "test.txt" there to read MyProgram.exe from test.txt.
---------------------------------------------------
To make to your Form a Closing Event Handler, you can do it by design mode, in the form properties panel, click on the Actions icon, then choose an event and write in the event textbox (giving a name) Form_MyEvent, Like: Form_Load, Form_Closing, Form_Closed, Form_MouseMove.........
---------------------------------------------------
Then see the "System.IO" what does has.
example: System.IO.File.Delete("C:\DeleteThisNow.doc") 'Delete a File
example2: System.IO.File.Create("C:\NewFile.exe") 'Create New File
---------------------------------------------------
Lets Save your settings by this code.
Now add to your form a button, by double clicking on the button, visual will create a Click event to your button.
then make to the button this action code:
Expand|Select|Wrap|Line Numbers
  1. Try
  2.                     If (My.Computer.FileSystem.FileExists("C:\test.txt")) Then
  3.                         MessageBox.Show("True Check was Arealy saved")
  4.                     End If
  5.  
  6.                     If (My.Computer.FileSystem.FileExists("C:\test.txt").Equals(False)) Then
  7.                         System.IO.File.Create("../../Settings Savers/CheckBox1.txt")
  8.                     End If
  9.                 End If
  10.  
  11.  
  12. If (CheckBox1.Checked = False) Then
  13.                     If (My.Computer.FileSystem.FileExists("C:\test.txt")) Then
  14.                         My.Computer.FileSystem.DeleteFile("C:\test.txt")
  15.                     End If
  16.  
  17.                     If (My.Computer.FileSystem.FileExists("C:\test.txt").Equals(False)) Then
  18.                         MessageBox.Show("False Check was Saved!")
  19.                     End If
  20.                 End If
  21.  
  22.                 Catch ex As Exception
  23.  
  24. End Try
  25.  
But the problem is sometimes you may Click on the Button 4 or 5 times to do this action.
---------------------------------------------------
Lets create a messagebox shown when you closing the form.
after you make to the form the closing handler, put(copy)this code inside the handle.
lets start:
Expand|Select|Wrap|Line Numbers
  1. Dim Message As String = "Save your settings?"
  2. Dim Caption As String = "Attention!"
  3. Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo                     Dim Result As DialogResult
  4. Result = MessageBox.Show(Message, Caption, Buttons)
  5. If Result = DialogResult.Yes Then
  6.     'Put the Save Settings Code
  7. End If
  8.  
Here we say, when you close the form, then will shown a messagebox with two buttons (Yes, No) if you choose Yes button,
Yes button will do the action up mean this action code:
If Result = DialogResult.Yes Then 'you see DialogResult.Yes, you can put No
'Put the Save Settings Code
End If
---------------------------------------------------
We finish, NOTHING IS HARD JUST TAKE TIME.
Good Luck.
May 12 '07 #15

Atran
100+
P: 319
To Learn VB, Download Free Ebooks from this site:
http://www.getebooks4free.com/dot_net_free_ebooks.html
May 12 '07 #16

Atran
100+
P: 319
Try Command:
In my 15th Post I write:
Expand|Select|Wrap|Line Numbers
  1.  Try
  2.  
  3.     If (My.Computer.FileSystem.FileExists("C:\test.txt")) Then
  4.                         CheckBox1.Checked = Flase
  5.     End If
  6.  
  7.    Catch ex As Exception
  8.  
  9. End Try
  10.  
If the test.txt file not found that will be error, then you can use Try to try.
May 12 '07 #17

Atran
100+
P: 319
I forget to till to you how to create a text file, see this code:
Expand|Select|Wrap|Line Numbers
  1. System.IO.File.Create("C:\Hello.bmp")
  2.  
or Using this:
Expand|Select|Wrap|Line Numbers
  1. System.IO.File.CreateText("C:\Hello.txt")
  2.  
Hope this Tutorial was Helpful.
May 12 '07 #18

Post your reply

Sign in to post your reply or Sign up for a free account.