Radio Buttons - How Do I Make Them Stay Checked When On Another Form?

HI,

I am currently having a problem with my project, i have two forms, frmSettings and frmMain,

i have 1 check box and 4 radiobuttons on the frmSettings form, and i want frmMain to perform actions based on which radiobutton is checked, for example if radio 1 is checked then play music "test1" if radio 2 then play music"test2" and so on, however once i close frmsettings it sets the values back to the original state,

is there a way of saving the current state of the radiobuttons so that the next time the form is displayed they are as they were when last left.

i am trying to create a screensaver, and the frmsettings is only displayed when the user clicks on the preview button in the "windows" screensaver settings and is then closed again to allow the user to carry on using windows,

any help is much appreciated

cheers

[909 byte] By [karate_kid007] at [2008-1-9]
# 1

Well the easy way would be to hide the form rather than closing it.

If you need to close it then you could use My.Settings to save the number of the selected radiobutton when you close it and reset it when you load it again.

In Projects | Properties | Settings create an Integer setting to store the number of the checked radiobutton. Access the setting using My.Settings.SettingName. Check which button is selected in the form.closing event and save the number to the setting. Set the same button to checked in the form.load event.

Dave299 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

There are many ways to do it.

1. Create an independant class outside of both forms in your project.

2. In this class, create your own schema of writing values and reading values from the class.

On the form_Closed event of one form, it will write to this class...and the FormLoad event of the next form will read the value from the class.

Note: These settings will only be available at runtime unless you find a clever way to set My.Setting.

Does this make sense?

Adamus

AdamusTurner at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

hi mate thanks for the quick reply,

it kinda makes sence, sounds a bit advanced (probably the terminalogy you have used lol) "schema" herd it not sure what it means thought but wont be too hard to find out/

i'll give it a try see how i get on.

cheers

luke

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 4

hi mate,

thanks for the quick reply,

i will try your suggestions and let you know how i get on

many thanks

luke

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 5
another way is with property binding to a setting in my.settings . You can either create the setting first on the settings tab of your project properties page or do it this way . Choose your radiobutton and then in the property grid drop down the application settings at the top . Click in the space beside property binding . In the small property grid that opens up scroll to the checked property and click in the space beside it . Drop the dropdown box down and you will see a small box with new at the bottom . If you premake settings they should be on the list if they are the right type if not press the new link and create a new setting . Leave the scope as user and give your setting a name and possibly a default value but that is not needed . Save that and then the checked property of your radiobutton will be bound to that setting . In your project settings make sure you have save settings on exit checked . Just to be sure create a sub for the check changed event of the radiobutton(s) and put my.settings.save in it to save the settings right away . You need a separete setting for each radiobutton . As long as you make sure the setting was saved you can read it's value even if the other form is closed by reading it from my.settings . I mean if the setting name was RB1 then you can see if my.settings.RB1 is true or false . For some items like a radio button just below property binding in the property grid in application setting there is some common properties already there waiting to se be set . For a radio button the checked property and the text property are there . That is quicker then clicking on the property binding and useing the small grid to scroll to those properties .
bdbodger at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 6
bdbodger wrote:
another way is with property binding to a setting in my.settings . You can either create the setting first on the settings tab of your project properties page or do it this way . Choose your radiobutton and then in the property grid drop down the application settings at the top . Click in the space beside property binding . In the small property grid that opens up scroll to the checked property and click in the space beside it . Drop the dropdown box down and you will see a small box with new at the bottom . If you premake settings they should be on the list if they are the right type if not press the new link and create a new setting . Leave the scope as user and give your setting a name and possibly a default value but that is not needed . Save that and then the checked property of your radiobutton will be bound to that setting . In your project settings make sure you have save settings on exit checked . Just to be sure create a sub for the check changed event of the radiobutton(s) and put my.settings.save in it to save the settings right away . You need a separete setting for each radiobutton . As long as you make sure the setting was saved you can read it's value even if the other form is closed by reading it from my.settings . I mean if the setting name was RB1 then you can see if my.settings.RB1 is true or false . For some items like a radio button just below property binding in the property grid in application setting there is some common properties already there waiting to se be set . For a radio button the checked property and the text property are there . That is quicker then clicking on the property binding and useing the small grid to scroll to those properties .

hi mate, this does the trick brilliantly, thank you for your time and advice.

the only other thing is that when i go to check the radio button to change its status i have to click it twice for it to change its check state, the first time i click it sets all radiobuttons to unchecked and then when i click again it it checks the one selected any ideas as to how i can make it change first time. thanks

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 7

Must be checking its setting on the first click . I guess you can use an event like the click event to change it in code on the first click and make sure the setting gets saved .

bdbodger at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 8

I'm afraid this solution doesn't work at all for me. Not only is there the problem with having to click twice to change a button but the settings are not reloaded correctly when the form reloads.

I found it much simpler to use a single String Setting called "Button" and add the following code:

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged

Dim RB As RadioButton = CType(sender, RadioButton)

If RB.Checked Then

My.Settings.Button = RB.Name

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

CType(Controls(My.Settings.Button), RadioButton).Checked = True

End Sub

Dave299 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 9
Dave299 wrote:

I'm afraid this solution doesn't work at all for me. Not only is there the problem with having to click twice to change a button but the settings are not reloaded correctly when the form reloads.

I found it much simpler to use a single String Setting called "Button" and add the following code:

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged

Dim RB As RadioButton = CType(sender, RadioButton)

If RB.Checked Then

My.Settings.Button = RB.Name

End If

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

CType(Controls(My.Settings.Button), RadioButton).Checked = True

End Sub

hi mate, i have tried this code but i keep getting a object reference not set to an instance of an oject? have i not coded something corrctly

Code Snippet

Public Class Form3

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

CType(Controls(My.Settings.button), RadioButton).Checked = True

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged

Dim RB As RadioButton = CType(sender, RadioButton)

If RB.Checked Then

My.Settings.button = RB.Name

End If

End Sub

End Class

do i need to put the radiobutton check changed data in all the radiobuttos checkchanged events,

i have created the string "button" in the project properties and have set it as a string and set the scope to user, do i need to attach it to the rdiobuttons them selfs?

cheers

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 10

You don't say where you get the error but I presume it is on this line:

CType(Controls(My.Settings.Button), RadioButton).Checked = True

If so it will be because you didn't set a default value when you created the setting. Put in a default value of RadioButton1 (or 2,3, or 4) and you should find it works ok.

If you want other code in the CheckedChanged event handlers then you have three options

1. Add the code above to all the handlers.

2. Put the code in a separate sub and call it from all the handlers

3. Use AddHandler to add the sub you made in 2 to all the radiobuttons

The setting is "standalone" - you don't need to bind it to the radiobuttons as you did in the other solution.

Dave299 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 11

dave299,

spot on mate that was the problem, it now works brilliantly, thank you for you time is been much appreciated

cheers

luke.

p.s im now gonna try and add a check box to this for so if it is checkd then the radiobuttos are enables and radiobutton1 is selected and if checkbox is not chcked then the radiobuttons are not enabled, should be straight forward but i'll post a comment here if i need help.

many thanks again.

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 12

hi dave229,

why are the easy things so difficult? i've been trying for ages to get the checkbox to work but can i, no...

all i want it to do is on the settings form which all the radiobuttons are located to have a checkbox which will either make the radiobuttons enabled or disabled, i've managed to make the turn on and off using the checkbox checkchanged event, and ive set up another setting called music_status, its a boolean and the value is set to true,

i have it so when it is unchecked the value of music_status is false and there for disables the radiobuttons, i've made sure i save the settings of the form close event but when i load it again the radiobuttons are not disabled

bascally its not saving the setting music_status

any ideas.

cheers

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 13

You haven't shown me any code so it's hard to see what's wrong. However this seems to work ok.

Public Class Form1

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

My.Settings.Music_Status = CheckBox1.Checked

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

CType(Controls(My.Settings.Button), RadioButton).Checked = True

CheckBox1.Checked = My.Settings.Music_Status

EnableRadioButtons(CheckBox1.Checked)

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged

Dim RB As RadioButton = CType(sender, RadioButton)

If RB.Checked Then

My.Settings.Button = RB.Name

End If

End Sub

Private Sub CheckBox1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

EnableRadioButtons(CheckBox1.Checked)

End Sub

Private Sub EnableRadioButtons(ByVal Value As Boolean)

If Value Then

RadioButton1.Enabled = True

RadioButton2.Enabled = True

RadioButton3.Enabled = True

RadioButton4.Enabled = True

Else

RadioButton1.Enabled = False

RadioButton2.Enabled = False

RadioButton3.Enabled = False

RadioButton4.Enabled = False

End If

End Sub

End Class

Dave299 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 14

dave 299,

thanks once again, the code worked perfectly, i really appreciate the time you have spent helping me through out this project,

cheers

luke

karate_kid007 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...