Results 1 to 7 of 7

Thread: [RESOLVED] How to save settings for UserControl(s)?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Resolved [RESOLVED] How to save settings for UserControl(s)?

    Hi all.
    Saving parameters for keeping data after shutting down is possible through Project>Settings.
    I already used several items to save (fonts, checkboxes booleans, strings, etc.)

    Code overview would look like this:
    Code:
    Private my_digit1 As Boolean
    
    Public Sub New()
            InitializeComponent()
            InitializeSettings()
    End Sub
    
     Private Sub InitializeSettings()
            my_digit1 = My.Settings.digitm1
    End Sub
    *Some graphical settings were also done.
    - (ApplicationSettings)
    (PropertyBindings)
    Value: digitm1


    The problem is, how can I save some UserControl array, control values in settings and recall them in every opening?

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

    Re: How to save settings for UserControl(s)?

    Are you saying, without actually saying, that you will have an unknown number of instances of this user control type and you want to save the configuration of each one? If so then you won't be able to use binding. You could still use settings though, if you were to use a single setting to store some XML that contained the configuration for all the controls. You'd have to write code to save the property values from the controls to the XML and to load it again. It's basically a manual process, just as you'd expect it to be, with the decision to be made being where to store the data.

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: How to save settings for UserControl(s)?

    I'm OK with manual ways, but I'm not sure how to do it.

    There are 24 User controls, each containing 2 CheckBoxes and 2 NumericUpDowns, 96 items to save overall.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to save settings for UserControl(s)?

    So you do know exactly what there is at design time, so you can just bind the properties like you would for any other control, so do that. You obviously need to add the properties in order to be able to bind them. You just add a pass-through property to the user control for each child control property to need to access, e.g.
    vb.net Code:
    1. Public Property CheckBox1Checked As Boolean
    2.     Get
    3.         Return CheckBox1.Checked
    4.     End Get
    5.     Set
    6.         CheckBox1.Checked = value
    7.     End Set
    8. End Property
    It's a property like any other, so you bind it like any other.

  5. #5

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    354

    Re: How to save settings for UserControl(s)?

    Thanks, It works.
    There is a saying: If you are doing something repetitious in programming, you're doing it wrong. I'm copy-pasting a satisfactorily working code, for 96 items, 3 time (calling, saving, opening) I kinda feel like a bit stoopid. But things are working properly now...

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: How to save settings for UserControl(s)?

    Quote Originally Posted by pourkascheff View Post
    Thanks, It works.
    There is a saying: If you are doing something repetitious in programming, you're doing it wrong. I'm copy-pasting a satisfactorily working code, for 96 items, 3 time (calling, saving, opening) I kinda feel like a bit stoopid. But things are working properly now...
    Um, that's not what I said to do so it would appear that that saying is rather apt.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: How to save settings for UserControl(s)?

    What you might consider is making up a class that contains all the settings, either per control or for the whole thing. It sounds like you aren't saving all that many properties for the controls, so one per control might be the most extensible solution. You can then make the class XML serializable such that the serialization/deserialization is simpler, or you could even go to binary serialization which has advantages (smaller, MUCH harder to read and change outside of the program) and disadvantages (MUCH harder to read and change outside of the program...which has other implications).
    My usual boring signature: Nothing

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