Results 1 to 9 of 9

Thread: [RESOLVED] Can't get radio buttons to save in My.Settings

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Resolved [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

    Does anyone know what I'm doing wrong?
    thanks

  2. #2
    Lively Member
    Join Date
    May 2009
    Posts
    69

    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.

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

    Re: Can't get radio buttons to save in My.Settings

    I would suggest that you define your own enumeration for this rather than using a number. Check out the attached example.
    Attached Files Attached Files
    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

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    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

    Quote Originally Posted by jmcilhinney View Post
    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.
    Last edited by 4xzer0; Jul 9th, 2009 at 10:42 AM.

  5. #5
    Hyperactive Member Zeljko's Avatar
    Join Date
    Oct 2006
    Location
    Internet
    Posts
    441

    Re: Can't get radio buttons to save in My.Settings

    I use this for my code and it works:
    Vb.net Code:
    1. 'In RadioButtonTip_CheckedChanged event...
    2. If RadioButtonTip.Checked = True Then
    3.    My.Settings.RadBtnTip = 1
    4. Else
    5.    My.Settings.RadBtnTip = 2
    6. End If
    7.  
    8. 'In Form_Load event...
    9. If My.Settings.RadBtnTip = 1 Then
    10.    RadioButtonTip.Checked = True
    11. Else
    12.    RadioButtonTip.Checked = False
    13. End If
    1. If this post helped you, please Rate it = That's You, saying Thanks, to Me ...Left side of this post: [Rate this post]
    2. Mark this Thread Resolved if your question has been answered That's You, saying Thanks, to Group ...Menu on top of your original Post: [Thread Tools]>[Mark Thread Resolved]
    3.
    Check my site: www.er-ef.netCheck my snippets: Get installed .NET versionsRegex extractingJoin hierarchically nested Datatables in one flattened Datatable


  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Can't get radio buttons to save in My.Settings

    Quote Originally Posted by Zeljko View Post
    I use this for my code and it works:
    Vb.net Code:
    1. 'In RadioButtonTip_CheckedChanged event...
    2. If RadioButtonTip.Checked = True Then
    3.    My.Settings.RadBtnTip = 1
    4. Else
    5.    My.Settings.RadBtnTip = 2
    6. End If
    7.  
    8. 'In Form_Load event...
    9. If My.Settings.RadBtnTip = 1 Then
    10.    RadioButtonTip.Checked = True
    11. Else
    12.    RadioButtonTip.Checked = False
    13. End If
    yeah this is what I had originally but it keeps going back to the default value after the app closes

  7. #7
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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:
    1. Private Sub Form2_FormClosing _
    2.     (ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    3.     Handles Me.FormClosing
    4.         My.Settings.Radio1 = RadioButton1.Checked
    5.         My.Settings.Radio2 = RadioButton2.Checked
    6.     End Sub
    7.  
    8.     Private Sub Form2_Load _
    9.     (ByVal sender As System.Object, ByVal e As System.EventArgs) _
    10.     Handles MyBase.Load
    11.         RadioButton1.Checked = My.Settings.Radio1
    12.         RadioButton2.Checked = My.Settings.Radio2
    13.     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!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    160

    Re: Can't get radio buttons to save in My.Settings

    Quote Originally Posted by tassa View Post
    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:
    1. Private Sub Form2_FormClosing _
    2.     (ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) _
    3.     Handles Me.FormClosing
    4.         My.Settings.Radio1 = RadioButton1.Checked
    5.         My.Settings.Radio2 = RadioButton2.Checked
    6.     End Sub
    7.  
    8.     Private Sub Form2_Load _
    9.     (ByVal sender As System.Object, ByVal e As System.EventArgs) _
    10.     Handles MyBase.Load
    11.         RadioButton1.Checked = My.Settings.Radio1
    12.         RadioButton2.Checked = My.Settings.Radio2
    13.     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

  9. #9
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

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