Save checkbox settings on close

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Lazareth
    New Member
    • Jan 2007
    • 29

    Save checkbox settings on close

    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.
  • Atran
    Contributor
    • May 2007
    • 319

    #2
    First put to your Form, FormClosing Event Handler, or FormClosed Event Handler, or not do that now, then Check this Code:

    My.Settings.Sav e()

    I hope this is helpful.

    Comment

    • Lazareth
      New Member
      • Jan 2007
      • 29

      #3
      Hi Atran

      I thought it was going to be that simple too.

      I've tried including my.settings.sav e on form closing and also added my.settings.rel oad on form load but no joy.

      Is there something else I have to do to make this setting work?

      Comment

      • Lazareth
        New Member
        • Jan 2007
        • 29

        #4
        Can anyone please help me with this.

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

        Any help appreciated

        Comment

        • Atran
          Contributor
          • May 2007
          • 319

          #5
          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.

          Comment

          • Atran
            Contributor
            • May 2007
            • 319

            #6
            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......... ......

            Comment

            • Atran
              Contributor
              • May 2007
              • 319

              #7
              If you want I can send you the code........... ............... ..... Answer me.

              Comment

              • Lazareth
                New Member
                • Jan 2007
                • 29

                #8
                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

                Comment

                • Atran
                  Contributor
                  • May 2007
                  • 319

                  #9
                  Originally posted by Lazareth
                  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!

                  Comment

                  • TRScheel
                    Recognized Expert Contributor
                    • Apr 2007
                    • 638

                    #10
                    Originally posted by Lazareth
                    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.

                    Code:
                    Dim XMLDocument As System.Xml.XmlDocument = New System.Xml.XmlDocument()
                            Dim XMLParentElement As System.Xml.XmlElement = XMLDocument.CreateElement("CheckBoxes")
                            XMLDocument.AppendChild(XMLParentElement)
                    
                            'Foreach checkbox do this:
                            Dim XMLNode As System.Xml.XmlNode = XMLDocument.CreateNode(Xml.XmlNodeType.Element, Nothing, "TheCheckBox", Nothing)
                            Dim XMLAttribute As System.Xml.XmlAttribute = XMLDocument.CreateAttribute("Checked")
                            XMLAttribute.Value = "true"
                            XMLNode.Attributes.Append(XMLAttribute)
                            XMLParentElement.AppendChild(XMLNode)
                            'Till here, again, foreach check box
                    
                            Dim SWriter As System.IO.StreamWriter = New System.IO.StreamWriter("Mylocation.xml")
                            SWriter.Write(XMLDocument.InnerXml)
                            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.Loa dXML

                    Comment

                    • Atran
                      Contributor
                      • May 2007
                      • 319

                      #11
                      Originally posted by Lazareth
                      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.
                      ------------------------------------------------------------------------------------------------------------------
                      Code:
                      Imports System.IO
                      Public Class Form1
                      
                          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                      
                      
                              'Let's Start form Here:
                              'Read carefully to understand the code, it is easy, but long.
                              'When you want to run this, open the Settings Savers Folder and Click the Button and see what is happen in the Folder.
                              '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.
                              '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.
                              ''''''''''''''''''''''''''''''''
                      
                              Try
                                  If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then 'This mean "../../" in your project path, in "My Application/My Application/"
                                      CheckBox1.Checked = True
                                  End If
                              Catch ex As Exception
                              End Try
                      
                              ''''''''''''''''''''''''''''''''
                      
                              Try
                                  If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
                                      CheckBox1.Checked = False
                                  End If
                              Catch ex As Exception
                              End Try
                      
                              '''''''''''''''''''''''''''''''''
                      
                              Try
                                  If (My.Computer.FileSystem.FileExists("../../Settings Savers/TextBox1.txt")) Then
                                      TextBox1.Text = "File Found: " + My.Computer.FileSystem.ReadAllText("../../Settings Savers/TextBox1.txt")
                                  End If
                              Catch ex As Exception
                              End Try
                      
                              '''''''''''''''''''''''''''''''''
                      
                              Try
                                  If (My.Computer.FileSystem.FileExists("../../Settings Savers/TextBox1.txt").Equals(False)) Then
                                      TextBox1.Text = "Not Found!"
                                  End If
                              Catch ex As Exception
                              End Try
                      
                              '''''''''''''''''''''''''''''''''
                      
                          End Sub
                      
                          Private Sub SaveSettingsButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSettingsButton.Click
                      
                              Try
                      
                                  If (CheckBox1.Checked = True) Then
                                      If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
                                          MessageBox.Show("True Check was Arealy saved")
                                      End If
                      
                                      If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
                                          System.IO.File.Create("../../Settings Savers/CheckBox1.txt")     'Create The CheckBox1.txt File
                                      End If
                                  End If
                      
                                  '''''''''''''''''''''''''''''''''
                      
                                  If (CheckBox1.Checked = False) Then
                                      If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
                                          My.Computer.FileSystem.DeleteFile("../../Settings Savers/CheckBox1.txt")
                                      End If
                      
                                      If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
                                          MessageBox.Show("False Check was Saved!")
                                      End If
                                  End If
                      
                              Catch ex As Exception
                                  Return
                              End Try
                      
                          End Sub
                      
                          Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
                      
                              Try
                      
                                  Dim Message As String = "Save your settings?"
                                  Dim Caption As String = "Attention!"
                                  Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo
                                  Dim Result As DialogResult
                                  Result = MessageBox.Show(Message, Caption, Buttons)
                                  If Result = DialogResult.Yes Then
                                      If (CheckBox1.Checked = True) Then
                                          If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
                                              MessageBox.Show("True Check was Arealy saved")
                                          End If
                      
                                          If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
                                              System.IO.File.Create("../../Settings Savers/CheckBox1.txt")     'Create The CheckBox1.txt File
                                          End If
                                      End If
                      
                                      '''''''''''''''''''''''''''''''''
                      
                                      If (CheckBox1.Checked = False) Then
                                          If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt")) Then
                                              My.Computer.FileSystem.DeleteFile("../../Settings Savers/CheckBox1.txt")
                                          End If
                      
                                          If (My.Computer.FileSystem.FileExists("../../Settings Savers/CheckBox1.txt").Equals(False)) Then
                                              MessageBox.Show("False Check was Saved!")
                                          End If
                                      End If
                                  End If
                      
                              Catch ex As Exception
                      
                              End Try
                          End Sub
                      End Class
                      Last edited by MMcCarthy; May 11 '07, 11:32 PM. Reason: email removed - against site rules

                      Comment

                      • Atran
                        Contributor
                        • May 2007
                        • 319

                        #12
                        Check this site:
                        http://www.thescripts. com/forum/thread643188.ht ml

                        Comment

                        • Atran
                          Contributor
                          • May 2007
                          • 319

                          #13
                          Originally posted by Lazareth
                          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.

                          Comment

                          • Lazareth
                            New Member
                            • Jan 2007
                            • 29

                            #14
                            Originally posted by Atran
                            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?

                            Comment

                            • Atran
                              Contributor
                              • May 2007
                              • 319

                              #15
                              Tutorial, Step by Step.

                              Originally posted by Lazareth
                              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:
                              Code:
                              If(My.Computer.FileSystem.FileExists("C:\test.txt")) Then
                                   CheckBox1.Checked = True
                              End If
                              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:
                              Code:
                              If(My.Computer.FileSystem.FileExists("C:\test.txt").Equals(Flase)) Then
                                   CheckBox1.Checked = Flase
                              End If
                              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:
                              Code:
                              If(My.Computer.FileSystem.FileExists("C:\test.txt")) Then
                                   CheckBox1.Checked = True
                              End If
                              Then you can fix that by making Try Command:
                              Code:
                              Try
                                   
                                   If(My.Computer.FileSystem.FileExists("C:\test.txt")) Then
                                        CheckBox1.Checked = True
                                   End If
                                   
                                   Catch ex As Exception
                              
                              End Try
                              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.tx t" 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:\Dele teThisNow.doc") 'Delete a File
                              example2: System.IO.File. Create("C:\NewF ile.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:
                              Code:
                              Try
                                                  If (My.Computer.FileSystem.FileExists("C:\test.txt")) Then
                                                      MessageBox.Show("True Check was Arealy saved")
                                                  End If
                              
                                                  If (My.Computer.FileSystem.FileExists("C:\test.txt").Equals(False)) Then
                                                      System.IO.File.Create("../../Settings Savers/CheckBox1.txt")
                                                  End If
                                              End If
                              
                              
                              If (CheckBox1.Checked = False) Then
                                                  If (My.Computer.FileSystem.FileExists("C:\test.txt")) Then
                                                      My.Computer.FileSystem.DeleteFile("C:\test.txt")
                                                  End If
                              
                                                  If (My.Computer.FileSystem.FileExists("C:\test.txt").Equals(False)) Then
                                                      MessageBox.Show("False Check was Saved!")
                                                  End If
                                              End If
                                              
                                              Catch ex As Exception
                              
                              End Try
                              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:
                              Code:
                              Dim Message As String = "Save your settings?"
                              Dim Caption As String = "Attention!"
                              Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo                     Dim Result As DialogResult
                              Result = MessageBox.Show(Message, Caption, Buttons)
                              If Result = DialogResult.Yes Then
                                  'Put the Save Settings Code
                              End If
                              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.Ye s Then 'you see DialogResult.Ye s, you can put No
                              'Put the Save Settings Code
                              End If
                              ---------------------------------------------------
                              We finish, NOTHING IS HARD JUST TAKE TIME.
                              Good Luck.

                              Comment

                              Working...