[RESOLVED] Can't get radio buttons to save in My.Settings
Hi
I'm trying to save my radio button settings in My.Settings...
At the moment I save the setting in the CheckChanged event for each radio button like this:
Private Sub Option1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Option1.CheckedChanged
If Option1.Checked Then
My.Settings.Option = "1"
End If
End Sub
And then I load the setting in the form_load event, like this:
If My.Settings.Option = "1" Then
Option1.Checked = True
Elseif My.Settings.Option = "2" Then
Option2.Checked = True
..etc
Also in the formclosing event I do My.Settings.Save()
Now this seems to work fine while the program is running. If I do frmSettings.showdialog() and then change the radio button settings and then close the form, and then frmSettings.showdialog() again, the settings will load correctly (so I guess that means they were saved in My.Settings since the form_load event runs each time showdialog is used)
But if I close the program completely and reopen it, the settings will have changed back to the default settings
Re: Can't get radio buttons to save in My.Settings
Try removing:
Code:
My.settings.save
It's unnecessary. Probably not the issue but is still unnecessary. Anyway, are the settings integers? If so try:
Code:
My.Settings.Option = 1
Instead of:
Code:
My.Settings.Option = "1"
Other than that I have no idea.
Edit: Okay I tested and removed the quoating of the numbers, had to change the "My.Settings.Option" to "My.Settings.Option1" as "Option" was invalid and stored "Option1" as an Integer. And it worked for me then. Maybe the issue for you is the quoting.
Re: Can't get radio buttons to save in My.Settings
hm PR0 6AM3R well I'm not actually using the setting "Option" and the value "1" anyway I just wrote that to try and make my problem easier to understand
Originally Posted by jmcilhinney
I would suggest that you define your own enumeration for this rather than using a number. Check out the attached example.
thanks for the example.. it looks like a good way of doing it - however my settings still weren't saved!
It seems that only the settings that I have bound to controls will save when the application shuts down.. anything I try with the radio buttons fails to save.
I tried the enumeration idea and once again it worked fine while the application was open (each time I did showdialog the my.settings value was loaded correctly). But when I closed the app and restarted the setting had changed back to its default value. Also I had to add a try catch on the dictionary.add lines because any time I did fSettings.showdialog after the first it threw an exception that the keys had already been added.. but I guess that's got nothing to do with the problem :s I have a feeling it could be something to do with the way my forms work? I have a frmMain and a frmSettings (with the radio buttons). On frmMain I have a global Dim fSettings as new frmSettings, and then I do fSettings.showdialog() each time the settings button is clicked on the main form. Could it be somehow resetting to the default value when the settings form is closed? But as I said before all the my.settings that are bound to controls on the settings form save fine after the app is closed.
Re: Can't get radio buttons to save in My.Settings
If form2 shows when you click a button in form1 then you should then save the radiobutton.checked when form2 is closing... That way every time the form2 closes it'll edit RadBtnTip...
Something like this I mean:
vb.net Code:
Private Sub Form2_FormClosing _
(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
My.Settings.Radio1 = RadioButton1.Checked
My.Settings.Radio2 = RadioButton2.Checked
End Sub
Private Sub Form2_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
RadioButton1.Checked = My.Settings.Radio1
RadioButton2.Checked = My.Settings.Radio2
End Sub
Radio1 and Radio2 are declared as booleans in the project settings, and the default value for both of them is set to false...
This code snippet worked for me, try it out
"In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
Niklaus E. Wirth
Rate any post that helped you, it's a good way of saying thanks
Please specify your Visual Studio Version!
Re: Can't get radio buttons to save in My.Settings
Originally Posted by tassa
If form2 shows when you click a button in form1 then you should then save the radiobutton.checked when form2 is closing... That way every time the form2 closes it'll edit RadBtnTip...
Something like this I mean:
vb.net Code:
Private Sub Form2_FormClosing _
(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles Me.FormClosing
My.Settings.Radio1 = RadioButton1.Checked
My.Settings.Radio2 = RadioButton2.Checked
End Sub
Private Sub Form2_Load _
(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
RadioButton1.Checked = My.Settings.Radio1
RadioButton2.Checked = My.Settings.Radio2
End Sub
Radio1 and Radio2 are declared as booleans in the project settings, and the default value for both of them is set to false...
This code snippet worked for me, try it out
Lol thanks... this simple way worked
Strange that the other methods wouldn't save for me! But I did change the my.setting variable for this method..maybe there was something wrong with the old setting I was using :s
Re: Can't get radio buttons to save in My.Settings
The radio button checked state depends on a boolean variable or in an integer... The integer should be 0 or 1 to work, since each one of these numbers represent one boolean value (true or false), you make the checked state dependant of a boolean. Is this clear? O_O, lol.
Sometimes I don't make myself clear... Hmm, I guess you get it, sometimes simple is better .
"In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
Niklaus E. Wirth
Rate any post that helped you, it's a good way of saying thanks
Please specify your Visual Studio Version!