-
Nov 21st, 2020, 04:42 PM
#1
Thread Starter
Junior Member
[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 which will change the checkbox, and to change the actual settings on the form. Here is a screenshot of my settings:
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!
-
Nov 21st, 2020, 09:07 PM
#2
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.
-
Nov 21st, 2020, 09:32 PM
#3
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:
Public Class Form2
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CheckBox1.Checked = My.Settings.Form1TopMost
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Settings.Form1TopMost = CheckBox1.Checked
My.Settings.Save()
DialogResult = DialogResult.OK
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
DialogResult = DialogResult.Cancel
End Sub
End Class
8. Open Form1 in the designer and add a Button.
9. Add the following code to Form1:
vb.net Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using dialogue As New Form2
Select Case dialogue.ShowDialog()
Case DialogResult.OK
If My.Settings.Form1TopMost Then
MessageBox.Show("Changes were saved. Form1 is now topmost.")
Else
MessageBox.Show("Changes were saved. Form1 is now NOT topmost.")
End If
Case DialogResult.Cancel
MessageBox.Show("No changes were saved.")
End Select
End Using
End Sub
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.
-
Nov 21st, 2020, 09:37 PM
#4
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.
-
Nov 22nd, 2020, 05:08 AM
#5
Thread Starter
Junior Member
Re: Saving and loading user settings
Just ignore that lol. Added it for no reason.
-
Nov 22nd, 2020, 05:16 AM
#6
Thread Starter
Junior Member
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?
-
Nov 22nd, 2020, 05:20 AM
#7
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.
-
Nov 22nd, 2020, 05:21 AM
#8
Thread Starter
Junior Member
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
-
Nov 22nd, 2020, 08:11 AM
#9
Thread Starter
Junior Member
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?
-
Nov 22nd, 2020, 08:43 AM
#10
Thread Starter
Junior Member
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?
-
Nov 22nd, 2020, 06:55 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|