Results 1 to 11 of 11

Thread: [RESOLVED] Saving and loading user settings

  1. #1

    Thread Starter
    Junior Member Phxantom16's Avatar
    Join Date
    Apr 2020
    Location
    London, England
    Posts
    18

    Resolved [RESOLVED] Saving and loading user settings

    Hey guys. I have an issue which is making me go crazy, I will try to explain as best as possible

    So I am making an options menu for my application where the user can enable and disable certain functions and features such as hiding the tray icon or hiding a tool strip menu item. The menu consists of a bunch of checkboxes. Upon checking the boxes and clicking the save button, the features will be enabled/disabled as expected for the corresponding box, however upon closing the form and the application, the checkboxes do not stay ticked/unticked I have done some code to do this which doesn't seem to work well. The following code is an option to enable/disable the application being top most:

    Code:
    If cbxAlwaysOnTop.Checked = True Then
                Form1.TopMost = True
                My.Settings.AlwaysOnTop = cbxAlwaysOnTop.Checked = True
                My.Settings.FormAlwaysOnTop = Form1.TopMost = True
                My.Settings.Save()
            End If
            If cbxAlwaysOnTop.Checked = False Then
                Form1.TopMost = False
                My.Settings.AlwaysOnTop = cbxAlwaysOnTop.Checked = False
                My.Settings.FormAlwaysOnTop = Form1.TopMost = False
                My.Settings.Save()
            End If
    Here I have specified what to do if it is checked or unchecked. I have also referenced the application settings which I have added. They are
    Code:
    AlwaysOnTop
    which will change the checkbox, and
    Code:
    FormAlwaysOnTop
    to change the actual settings on the form. Here is a screenshot of my settings:

    Name:  Screenshot 2020-11-21 213440.png
Views: 345
Size:  2.4 KB

    Upon opening the options menu I have this code run:

    Code:
    Private Sub options_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            My.Settings.Reload()
            My.Settings.Upgrade()
            cbxAlwaysOnTop.Checked = My.Settings.AlwaysOnTop
    Upon opening the main form I have this code run:

    Code:
    Me.TopMost = My.Settings.FormAlwaysOnTop
    When closing the options form and reopening it, the checkboxes do not change, same goes with closing and reopening the application as a whole.

    When closing the application and reopening it, the top most setting has not saved if the user checked the box.

    If I was to tick the top most checkbox, save it it will apply the setting to the form, but it will not save upon reopening the application and also the checkbox will not stay ticked.

    The same goes for unticking the checkbox, and saving it. The checkbox will still be ticked when opening the form again.


    So overall, the settings will not save and the checkboxes will not save.

    Seems complicated to me but hopefully someone can help. Hopefully I have explained everything.

    Cheers!

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

    Re: Saving and loading user settings

    Firstly, be aware that you can bind control properties to application settings. You can do this in the Properties window, using the (ApplicationSettings) node. That means not having to set properties manually based on setting throughout the application. The only place you need to get and set the settings values in in the settings dialogue.

    As for the issue, it sounds like the XML file that contains the user settings is either not being created or is being deleted when you build. You should find out exactly where that file is and watch it for yourself to see if this is the case. You should be able to find that information from the Settings page of the project properties.

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

    Re: Saving and loading user settings

    Actually, looking more closely at your code, I'm wondering whether you're breaking it yourself. Everything seems far more convoluted than is required. Try the following in a new project and see whether it works. If it does, implement the same principle in your current project.

    1. Create a new Windows Forms Application project.
    2. Add a second form to the project.
    3. Open the Settings page of the project properties and add a setting of type Boolean, named Form1TopMost and set to False.
    4. Open Form1 in the designer, open the Properties window, expand the (ApplicationSettiings) node, select the (PropertyBindings) node and click the browse (...) button.
    5. Select the TopMost property, click the drop-down arrow and select Form1TopMost, then click OK.

    At this stage, your Form1.TopMost property is no bound to your Form1TopMost setting. Any change to one will automatically affect the other. There's never a need for you to manually transfer data between them. Note that you could have created the setting while setting up the binding via the (New...) link, rather than creating it first.

    6. Open Form2 in the designer and add a CheckBox and two Buttons.
    7. Add the following code to Form2:
    vb.net Code:
    1. Public Class Form2
    2.  
    3.     Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    4.         CheckBox1.Checked = My.Settings.Form1TopMost
    5.     End Sub
    6.  
    7.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    8.         My.Settings.Form1TopMost = CheckBox1.Checked
    9.         My.Settings.Save()
    10.  
    11.         DialogResult = DialogResult.OK
    12.     End Sub
    13.  
    14.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    15.         DialogResult = DialogResult.Cancel
    16.     End Sub
    17.  
    18. End Class
    8. Open Form1 in the designer and add a Button.
    9. Add the following code to Form1:
    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4.         Using dialogue As New Form2
    5.             Select Case dialogue.ShowDialog()
    6.                 Case DialogResult.OK
    7.                     If My.Settings.Form1TopMost Then
    8.                         MessageBox.Show("Changes were saved. Form1 is now topmost.")
    9.                     Else
    10.                         MessageBox.Show("Changes were saved. Form1 is now NOT topmost.")
    11.                     End If
    12.                 Case DialogResult.Cancel
    13.                     MessageBox.Show("No changes were saved.")
    14.             End Select
    15.         End Using
    16.     End Sub
    17.  
    18. End Class
    You can now run the project and experiment with the dialogue and whether the setting is persisted across sessions. When I tested that, if I checked the CheckBox and clicked Button1, Form1 would be topmost. If I then unchecked the CheckBox and clicked Button1, Form1 would not be topmost, exactly as you'd expect. If I closed the application with Form1 topmost and then ran the project again, Form1 would still be topmost, indicating that the XML file containing the user settings was being retained across sessions.

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

    Re: Saving and loading user settings

    I just noticed that your question title is prefixed with VS Code but your screenshot indicates that you're using VS. Which is it? There are no visual project property pages in VS Code, as far as I'm aware.

  5. #5

    Thread Starter
    Junior Member Phxantom16's Avatar
    Join Date
    Apr 2020
    Location
    London, England
    Posts
    18

    Re: Saving and loading user settings

    Just ignore that lol. Added it for no reason.

  6. #6

    Thread Starter
    Junior Member Phxantom16's Avatar
    Join Date
    Apr 2020
    Location
    London, England
    Posts
    18

    Re: Saving and loading user settings

    Upon creating this new project to test and opening the settings page, I get a message saying "This project does not contain a default settings file. Click to create one". Upon clicking it I then get the following error message.

    Attachment 179349

    Any ideas?

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

    Re: Saving and loading user settings

    Your attachment is invalid.

    Are you definitely creating the correct project type, i.e. VB, WinForms App, .NET Framework? If so, maybe your VS installation is corrupt and you should perform a repair on it.

  8. #8

    Thread Starter
    Junior Member Phxantom16's Avatar
    Join Date
    Apr 2020
    Location
    London, England
    Posts
    18

    Re: Saving and loading user settings

    I will perform a repair on the installation and try again

    Here is the attachment again, hopefully it works

    Attachment 179350

  9. #9

    Thread Starter
    Junior Member Phxantom16's Avatar
    Join Date
    Apr 2020
    Location
    London, England
    Posts
    18

    Re: Saving and loading user settings

    Works very well! Thank you! Quick question, another one of my options is to disable the tray (notify) icon, so would I use the same kind of code to do this? I have tried with the same code and modified it to suit, but the tray seems to stay visible. Would I also use the same code if I was to hide tool strip menu items and labels on the form?

  10. #10

    Thread Starter
    Junior Member Phxantom16's Avatar
    Join Date
    Apr 2020
    Location
    London, England
    Posts
    18

    Re: Saving and loading user settings

    Works very well! Thank you! Quick question, another one of my options is to disable the tray (notify) icon, so would I use the same kind of code to do this? I have tried with the same code and modified it to suit, but the tray seems to stay visible. Would I also use the same code if I was to hide tool strip menu items and labels on the form?

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

    Re: Saving and loading user settings

    I just tested a NotifyIcon and, while it does appear to support binding, it doesn't seem to respond when a bound setting is changed. It did seem to read the correct value at startup but then didn't respond to changes during the session. I suspect that this is due to its being displayed outside of any form. You may just have to add an extra line of code to set the Visible property after ShowDialog returns.

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