Results 1 to 9 of 9

Thread: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    Hi everyone!
    i am back again for your great help!
    I have learn a lot thank to you! and i say you thank you indefinitely!
    ok, imy problem is i have a form with several pictures, labels , numericupdown,checkbox ,etc i would like a code to make a back up from this form and a code to restore the data ( numericdown,picture,and so on...) i found on net coding but i don't manage to do what i want!

    my research show me sql database but i didn' t understand the mecanism!

    SO i will be very grateful for a hand!
    thank you so much in advance for your answer!
    Last edited by danzey; Feb 14th, 2021 at 08:05 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    This really is too broad a question. You're not backing up a form. You're simply saving data. It's up to you how you want to do that: text file, serialisation, database, etc. Once you've decided on the method, then you need to research that method. It's not for us to teach you everything about databases and data access here. That's a broad topic and there's lots of information easily accessible so you need to do your research first, do what you can for yourself and then ask us when you encounter an actual issue. If you're unable to use a search engine effectively for some reason, there's a Database FAQ link in my signature below that provides links to some of the ADO.NET resources on this site.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    danzey,
    did you mean you want to make a backup from a form without any code in it just with controls? Its pretty simple. From another project just add existing item, select your desired form and you have backed it up!

    As for sql, I dont have any experiences with it so I cannot comment.
    Please dont forget to add good reputation if my advices were useful for you.
    How? Under this post there is "RATE THIS POST" button. Click on it.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    Quote Originally Posted by VB.NET Developer View Post
    danzey,
    did you mean you want to make a backup from a form without any code in it just with controls? Its pretty simple. From another project just add existing item, select your desired form and you have backed it up!

    As for sql, I dont have any experiences with it so I cannot comment.
    Looks like you’ve misunderstood the question. As JM understood it, and as I understood it, danzey is looking to save a form state, then recreate that state at a later time...

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    When I need to save my form data, I create a class with the appropriate properties linked to each data to save on the form, then create and instance of that class and save the instance in a xml file :

    Code:
    Public Sub Save_xml(ByVal my_instance As my_class)
    
          Dim filetosave As New SaveFileDialog With { .FileName = "name" }
          If filetosave.ShowDialog() = DialogResult.OK Then
             Dim Filename As String = filetosave.FileName
             Dim serialiseur As New XmlSerializer(GetType(my_class))
                Using fs As FileStream = File.Create(Filename)
                    serialiseur.Serialize(fs, my_instance)
                End Using
          End If
    
    End Sub
    Works well with numbers and strings but I never try with a picture as I usually save the pictures as separate files
    Last edited by Delaney; Feb 16th, 2021 at 05:49 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    Quote Originally Posted by Delaney View Post
    When I need to save my form data, I create a class with the appropriate properties linked to each data to save on the form, then create and instance of that class and save the instance in a xml file :

    Code:
    Public Sub Save_xml(ByVal my_instance As my_class)
    
          Dim filetosave As New SaveFileDialog With { .FileName = "name" }
          If filetosave.ShowDialog() = DialogResult.OK Then
             Dim Filename As String = filetosave.FileName
             Dim serialiseur As New XmlSerializer(GetType(my_class))
                Using fs As FileStream = File.Create(Filename)
                    serialiseur.Serialize(fs, my_instance)
                End Using
          End If
    
    End Sub
    Works well with numbers and strings but I never try with a picture as I usually save the pictures as separate files
    THANK all of us for your Answers , i like your idea(delaney! from france too!) but now the question is how can i restore or load in my form saved with the file xlm?

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    for the importation it is the same :
    Code:
    Public Function importation_xml() As my_class  
         Dim filetoopen As New OpenFileDialog
         If filetoopen.ShowDialog() = DialogResult.OK Then
             Dim Filename As String = filetoopen.FileName
             'open an instance of reading
             Dim str As Stream = File.OpenRead(Filename)
             Dim dsr As New XmlSerializer(GetType(my_class))
             Return CType(dsr.Deserialize(str), my_class)
             sr.Close()
         Else
             Return New my_class
         End If
       End Function
    I advice you to put both importation function and save sub in the class

    in your form, create a sub to load your data and call the function and link the properties of the class to the controls:

    Code:
    dim my_instance As  new my_class ' you may want to put that at the top of the form if you need to update the data in several other sub.
    my_instance= my_instance.importation_xml()
    thereafter an example :

    Code:
    with my_instance
       label_name.text=.name (and in my instance you have  : property name as string)
       label_age.text=.age.tostring (and in my instance you have  : property age as integer)
       etc.for each control you want to fill
    end with
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,370

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    @Delaney - Are you certain that you can serialize/deserialize a Form like that?

    The reason I ask is because my knee jerk reaction would be that it would serialize the Form properties but would have unintended side effects when deserializing the data back, especially for controls that you want to update a non-primitive property (like PictureBox.Image).

    Edit - Please do not misconstrue my question. I am not questioning your abilities as a programmer, I simply haven't serialized a Form before in a manner that you're describing. If it works, it is good to have in my back pocket.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: [HELP CODING BEGINNER] MAKE A BACK UP FROM A FORM AND RESTORE it later?

    Quote Originally Posted by dday9 View Post
    @Delaney - Are you certain that you can serialize/deserialize a Form like that?

    The reason I ask is because my knee jerk reaction would be that it would serialize the Form properties but would have unintended side effects when deserializing the data back, especially for controls that you want to update a non-primitive property (like PictureBox.Image).

    Edit - Please do not misconstrue my question. I am not questioning your abilities as a programmer, I simply haven't serialized a Form before in a manner that you're describing. If it works, it is good to have in my back pocket.
    Don't worry, you have the right to question my abilities as I am an amateur, I do a lot of error of coding (I still use msgbox ) and I have still a lot to learn.

    I don't serialize a form, I serialize a instance of a class I created and that contains all the information I need to use and save. I fill the data of the class with the info of the controls of the form (like textbox, label, etc.) or the reverse (fill the form textbox, label, combobox, etc. with the data from the class). For my applications, it is always strings and numbers and list (of strings or number) and other classes I created (I created a class called curve that contains a list of points, name of the curve, comment, etc. ). I never try to do it with a picturebox or a chart as I recreate the charts with my class "curve" and for the pictures, I save or export them as separate files. I can send to you some files of one of my applications if you want.

    PS : now, I see the confusion I created as I was speaking of the form data in post #5. I am sorry, It is a misuse of words. By form data I mean all the information that are in the controls I put on the form (texts, etc.). In fact I same much more than that as I use the form only as a User Interface and I do every data treatment and calculus in the specific class I create for that.
    Last edited by Delaney; Feb 18th, 2021 at 07:17 PM.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

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