|
-
Feb 25th, 2009, 08:33 PM
#1
Thread Starter
New Member
Saving Radio Buttons?
Hi there,
I have a system on my webbrowser that allows the user to choose their Search Engine out of a selection I organized. the down point is that I have it set so when they click save it will just hide, because if it closes the settings will be reset and not saved. However I want them to save. Here are my codes to the search button:
Code:
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
If SearchEngines.RadioButton1.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.google.com/search?hl=en&q=" & SearchBar.Text & "&btnG=Google+Search&meta=")
End If
If SearchEngines.RadioButton2.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://search.yahoo.com/search?p=" & SearchBar.Text & "&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8&fp_ip=IN&vc=")
End If
If SearchEngines.RadioButton3.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://images.google.ca/images?hl=en&q=" & SearchBar.Text)
End If
If SearchEngines.RadioButton4.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://en.wikipedia.org/wiki/Special:Search?search=" & SearchBar.Text)
End If
If SearchEngines.RadioButton6.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://video.google.com/videosearch?q=" & SearchBar.Text)
End If
If SearchEngines.RadioButton5.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.youtube.com/results?search_type=&search_query=" & SearchBar.Text)
End If
If SearchEngines.RadioButton7.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://www.ask.com/web?q=" & SearchBar.Text & "&search=&qsrc=0&o=0&l=dir")
End If
If SearchEngines.RadioButton8.Checked Then
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate("http://news.google.ca/news?hl=en&tab=ln&nolr=1&q=" & SearchBar.Text)
End If
End Sub
This is the code of save:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Hide()
End Sub
I would like it so it saves when the whole program is closed, so when they start up it's automatically set so they can search on their favourite search engine.
-
Feb 25th, 2009, 09:48 PM
#2
Re: Saving Radio Buttons?
Open the project and go to "Properties > NAME Properties" where NAME is the name of your project. Go to settings and create a variable called "SelectedEngine". Make it an integer and set its value to 1.
Go to the Load event of your form with the RadioButtons and type in:
vb Code:
Dim ctrl As New RadioButton ctrl.Name = "RadioButton" & My.MySettings.SelectedEngine ctrl.Checked = True
And put this on the Form Closing event:
vb Code:
For Each ctrl As Control In Controls If TypeOf ctrl Is RadioButton Then Dim rb As RadioButton = ctrl If rb.Checked = True Then My.Settings.SelectedEngine = rb.Name.Substring(11, 1) End If Next
Last edited by Vectris; Feb 25th, 2009 at 10:00 PM.
-
Feb 25th, 2009, 09:56 PM
#3
Thread Starter
New Member
Re: Saving Radio Buttons?
Thanks for the Reply! but I followed your instructions perfectly, yet it doesn't save, do I need to put My.Setting.Save some where?
This is my code so far:
Code:
Public Class Options
Private Sub SaveSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSearch.Click
Me.Close()
My.Settings.Save()
If RadioButton1.Checked Then
End If
If RadioButton2.Checked Then
End If
If RadioButton3.Checked Then
End If
If RadioButton4.Checked Then
End If
If RadioButton6.Checked Then
End If
If RadioButton5.Checked Then
End If
If RadioButton7.Checked Then
End If
If RadioButton8.Checked Then
End If
End Sub
Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ctrl As New RadioButton
ctrl.Name = "RadioButton" & My.Settings.SelectedEngine
ctrl.Checked = True
End Sub
-
Feb 25th, 2009, 10:02 PM
#4
Re: Saving Radio Buttons?
It appears you didn't add the Form Closing code, I don't see it on there. That's the part that would save it. Look at my previous post and at the bottom you'll see the second code I suggested you add. Also, what version of VB are you using, 2005 2008?
-
Feb 25th, 2009, 10:03 PM
#5
Thread Starter
New Member
Re: Saving Radio Buttons?
Sorry didn't copy whole code lol, you can notice the End Class isn't there either :P
Code:
Public Class Options
Private Sub SaveSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSearch.Click
Me.Close()
My.Settings.Save()
If RadioButton1.Checked Then
End If
If RadioButton2.Checked Then
End If
If RadioButton3.Checked Then
End If
If RadioButton4.Checked Then
End If
If RadioButton6.Checked Then
End If
If RadioButton5.Checked Then
End If
If RadioButton7.Checked Then
End If
If RadioButton8.Checked Then
End If
End Sub
Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ctrl As New RadioButton
ctrl.Name = "RadioButton" & My.Settings.SelectedEngine
ctrl.Checked = True
End Sub
Private Sub Options_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
For Each ctrl As Control In Controls
If TypeOf ctrl Is RadioButton Then
Dim rb As RadioButton = ctrl
If rb.Checked = True Then My.Settings.SelectedEngine = rb.Name.Substring(11, 1)
End If
Next
End Sub
End Class
-
Feb 25th, 2009, 10:06 PM
#6
Re: Saving Radio Buttons?
Ok take out the Me.Close() in the begging of the Save Click Event and put it at the end. Also take out My.Settings.Save and add it to the end of the Options_FormClosing Event.
Also go to the Project > NAME Properties window again and go to the Application tab. Check the box for Save My.Settings on Shutdown.
-
Feb 25th, 2009, 10:09 PM
#7
Thread Starter
New Member
Re: Saving Radio Buttons?
Okay still didn't work :S...
Code:
Public Class Options
Private Sub SaveSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSearch.Click
If RadioButton1.Checked Then
End If
If RadioButton2.Checked Then
End If
If RadioButton3.Checked Then
End If
If RadioButton4.Checked Then
End If
If RadioButton6.Checked Then
End If
If RadioButton5.Checked Then
End If
If RadioButton7.Checked Then
End If
If RadioButton8.Checked Then
End If
Me.Close()
End Sub
Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ctrl As New RadioButton
ctrl.Name = "RadioButton" & My.Settings.SelectedEngine
ctrl.Checked = True
End Sub
Private Sub Options_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
For Each ctrl As Control In Controls
If TypeOf ctrl Is RadioButton Then
Dim rb As RadioButton = ctrl
If rb.Checked = True Then My.Settings.SelectedEngine = rb.Name.Substring(11, 1)
End If
Next
My.Settings.Save()
End Sub
End Class
I Did the Save My.Settings on Shutdown and it's checked now.
-
Feb 25th, 2009, 10:10 PM
#8
Re: Saving Radio Buttons?
Ok never mind I found the problem. The Load Event code I gave you was wrong, use this instead.
vb Code:
For Each ctrl As Control In Controls If TypeOf ctrl Is RadioButton Then If ctrl.Name.Substring(11, 1) = My.Settings.SelectedEngine Then Dim rb As RadioButton = ctrl rb.Checked = True End If End If Next
Also you don't need the my.settings.save() at all as long as you check the box for save settings on shut down.
-
Feb 25th, 2009, 10:13 PM
#9
Thread Starter
New Member
Re: Saving Radio Buttons?
Hate to keep bringing you bad news.. didn't work once again.
Code:
Public Class Options
Private Sub SaveSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveSearch.Click
If RadioButton1.Checked Then
End If
If RadioButton2.Checked Then
End If
If RadioButton3.Checked Then
End If
If RadioButton4.Checked Then
End If
If RadioButton6.Checked Then
End If
If RadioButton5.Checked Then
End If
If RadioButton7.Checked Then
End If
If RadioButton8.Checked Then
End If
Me.Close()
End Sub
Private Sub Options_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctrl As Control In Controls
If TypeOf ctrl Is RadioButton Then
If ctrl.Name.Substring(11, 1) = My.Settings.SelectedEngine Then
Dim rb As RadioButton = ctrl
rb.Checked = True
End If
End If
Next
End Sub
Private Sub Options_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
For Each ctrl As Control In Controls
If TypeOf ctrl Is RadioButton Then
Dim rb As RadioButton = ctrl
If rb.Checked = True Then My.Settings.SelectedEngine = rb.Name.Substring(11, 1)
End If
Next
My.Settings.Save()
End Sub
End Class
-
Feb 25th, 2009, 10:17 PM
#10
Re: Saving Radio Buttons?
What version of Visual Basic are you using? I'm using 2005 and it works for me so it may be something other than the code.
All the RadioButtons are named "RadioButton1" 2 3 4 etc. right? And they are all on the Options form?
When you test it you are selecting one, closing the form, and then re-opening it right? If you opened it first by debugging then when you open it again you have to debug to get the same settings. If you go to the Bin folder then you need to go there to test it both times.
-
Feb 25th, 2009, 10:18 PM
#11
Thread Starter
New Member
Re: Saving Radio Buttons?
I'm using 2008, and Yes to all your questions. But Does it matter if google is my default search?
-
Feb 25th, 2009, 10:20 PM
#12
Re: Saving Radio Buttons?
 Originally Posted by NotMyRealName
Does it matter if google is my default search?
When your saving the settings I don't think that will matter. Why do you think it would?
Anyways, what happens when you open the form for the second time having selected a radio button previously, are any of them selected at all? Or is it always the same one?
-
Feb 25th, 2009, 10:21 PM
#13
Thread Starter
New Member
Re: Saving Radio Buttons?
Well I have radioButton 1 checked by default, so when I open options, and checked say number 3, close it I will open it and it will be 1 once more.
-
Feb 25th, 2009, 10:26 PM
#14
Re: Saving Radio Buttons?
Oh ok that's what's messing it up, if it's checked by default it overrides anything else you do at Load, I think. Make radioButton1 unchecked so that none of the radioButton's are checked by default. Then try it.
-
Feb 25th, 2009, 10:28 PM
#15
Thread Starter
New Member
Re: Saving Radio Buttons?
Okay now, I check one, click the button, it closes and i open it, and then they all unchecked.
-
Feb 25th, 2009, 10:31 PM
#16
Re: Saving Radio Buttons?
Well I'm out of ideas. That's so weird because it works fine for me on 2005, and if your not getting any errors with it in 2008 it should work the same. Someone else will probably be able to point out what is wrong or just give you a whole new suggestion. Sorry I couldn't be of further help.
-
Feb 25th, 2009, 10:38 PM
#17
Thread Starter
New Member
Re: Saving Radio Buttons?
thats fine your alot smarter then me ahaha! Well while your still online do you know how to open a form on a certain tab? On options i have two tabs general and search Engines
i want it to open on the second
Do you know\?
-
Feb 25th, 2009, 10:42 PM
#18
Re: Saving Radio Buttons?
I know that at least. Use:
Code:
TabControl1.SelectedIndex = 1
Where "TabControl1" is the name of your tab control, not the name of the tab page you want but the name of the whole tab control. 1 represents the second tab, 0 = first tab, 2 = 3rd tab, etc.
-
Feb 25th, 2009, 10:56 PM
#19
Thread Starter
New Member
Re: Saving Radio Buttons?
How do I go about it? I want to add it in a button, i tried putting Options.Show = (TabControl1.SelectedIndex = 1) I got a error under the Options.Show I tried without brackets, I got same error.
-
Feb 26th, 2009, 06:07 PM
#20
Re: Saving Radio Buttons?
No it works on it's own. Just go to the Click event for the button and paste that in, you don't need to add anything else to it.
EDIT: I think I misinterperted what you said.
What your trying to do is click a button, open a form, and upon the form opening, it should open to the second tab? But only when you open the form using that button.
Then on the Click Event for the first form, put
Code:
Options.Show
Options.TabControl.SelectedIndex = 1
That will open the form and then select the second tab on that form. Replace TabControl if the name of yours is different.
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
|