Join 358,485 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,186 people online right now.Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
radio buttons
52 Weeks of Code Challenge:Android
Week #11 of the 52 Weeks of Code Challenge is Android, you should give it a shot. Click Here!
Following is my code for a VB.Net project using (2) groups of radio keys: 1st group lists the states and the 2nd group lists the capitals.
Object: when one state and one capital from each group is matched up, either you receive a message stating you are correct or you are incorrect.
My code works as written, but I need some feedback as to how I can use less code to accomplish the same task. I know there has to be a less wordy way to make this work.
Thanks in advance for any recommendations.
raeNet
[Option Explicit On
Option Strict On
Public Class MainForm
' module-level string variables
Private capital As String
Private choice As String
Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
Me.Close()
End Sub
Private Sub SelectStateAndCapital()
' select capital for each state
If Me.xArkRadioButton.Checked And Me.xLittleRockRadioButton.Checked Then
choice = capital
ElseIf Me.xIllRadioButton.Checked And Me.xSpringfieldRadioButton.Checked Then
choice = capital
ElseIf Me.xKenRadioButton.Checked And Me.xFrankfortRadioButton.Checked Then
choice = capital
ElseIf Me.xOreRadioButton.Checked And Me.xSalemRadioButton.Checked Then
choice = capital
ElseIf Me.xWisRadioButton.Checked And Me.xMadisonRadioButton.Checked Then
choice = capital
End If
' select state for each capital
If Me.xFrankfortRadioButton.Checked And Me.xKenRadioButton.Checked Then
capital = choice
ElseIf Me.xLittleRockRadioButton.Checked And Me.xArkRadioButton.Checked Then
capital = CStr(True)
ElseIf Me.xMadisonRadioButton.Checked And Me.xWisRadioButton.Checked Then
capital = choice
ElseIf Me.xSalemRadioButton.Checked And Me.xOreRadioButton.Checked Then
capital = choice
ElseIf Me.xSpringfieldRadioButton.Checked And Me.xIllRadioButton.Checked Then
capital = choice
End If
End Sub
Private Sub ProcessStateRadioButtons(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles xArkRadioButton.Click, xIllRadioButton.Click, xKenRadioButton.Click, xOreRadioButton.Click, xWisRadioButton.Click
Call SelectStateAndCapital()
' clear message box
Me.xMsgLabel.Text = String.Empty
End Sub
Private Sub ProcessCapitalRadioButtons(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles xFrankfortRadioButton.Click, xLittleRockRadioButton.Click, xMadisonRadioButton.Click, xSalemRadioButton.Click, xSpringfieldRadioButton.Click
Call SelectStateAndCapital()
'clear message box
Me.xMsgLabel.Text = String.Empty
End Sub
Private Sub xVerifyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xVerifyButton.Click
Call SelectStateAndCapital()
If Me.xArkRadioButton.Checked And Me.xLittleRockRadioButton.Checked Then
Me.xMsgLabel.Text = "Correct"
Else
Me.xMsgLabel.Text = "Incorrect"
If Me.xIllRadioButton.Checked And Me.xSpringfieldRadioButton.Checked Then
Me.xMsgLabel.Text = "Correct"
Else
Me.xMsgLabel.Text = "Incorrect"
If Me.xKenRadioButton.Checked And Me.xFrankfortRadioButton.Checked Then
Me.xMsgLabel.Text = "Correct"
Else
Me.xMsgLabel.Text = "Incorrect"
If Me.xOreRadioButton.Checked And Me.xSalemRadioButton.Checked Then
Me.xMsgLabel.Text = "Correct"
Else
Me.xMsgLabel.Text = "Incorrect"
If Me.xWisRadioButton.Checked And Me.xMadisonRadioButton.Checked Then
Me.xMsgLabel.Text = "Correct"
Else
Me.xMsgLabel.Text = "Incorrect"
End If
End If
End If
End If
End If
End Sub
End Class]
[b]*Always use code blocks =>
This post has been edited by PsychoCoder: 03 November 2007 - 06:19 PM
Dream Kudos: 0 Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript
Re: radio buttons
Posted 03 November 2007 - 01:50 PM
There are a couple ways you could have tackled this. First being instead of using radio buttons to use something like two drop downs. One for states, one for capitals and then use the button to verify.
The second choice is to use something like the "tag" property of your radio button controls. This property is used for general information. In this property you could use something like a number (1 - 50 for your states) and then the same number for the state's capital (1-50 for the capitals). Then what you would do is during the verify event of your button read in the two selected controls and compare their tag values looking for the match.
If you wanted to even go shorter, you could create your radio buttons using a loop which creates the two radio buttons at a time (one state and one capital), assigns the counter to their tag properties, and then add them to the form in any order you chose.
But those are some ideas for you. Hope that helps!
There are a couple ways you could have tackled this. First being instead of using radio buttons to use something like two drop downs. One for states, one for capitals and then use the button to verify.
The second choice is to use something like the "tag" property of your radio button controls. This property is used for general information. In this property you could use something like a number (1 - 50 for your states) and then the same number for the state's capital (1-50 for the capitals). Then what you would do is during the verify event of your button read in the two selected controls and compare their tag values looking for the match.
If you wanted to even go shorter, you could create your radio buttons using a loop which creates the two radio buttons at a time (one state and one capital), assigns the counter to their tag properties, and then add them to the form in any order you chose.
But those are some ideas for you. Hope that helps!
Thank you for the suggestions; however, I'm quite the novice where VB.NET is concerned and haven't used or learned the tag property yet, so I'm not sure how receptive my prof. would be. This is an assignment, but as usual the textbook example doesn't resemble the assigned work, thus the reason for my excessive coding. If you have any other suggestions, I'm all ears. Don't get me wrong, I don't want someone to do the work for me, I just need some guidance.
I thought that in the verify control, I could somehow use the coding such as:
Dream Kudos: 0 Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript
Re: radio buttons
Posted 03 November 2007 - 08:16 PM
Well the way you have designed it you have essentially 100 radio buttons, that is a lot of checking. I have told you ways of simplifying the code by dynamically making the controls and such. The loop to make your controls would be roughly 10 to 20 lines.
Well the way you have designed it you have essentially 100 radio buttons, that is a lot of checking. I have told you ways of simplifying the code by dynamically making the controls and such. The loop to make your controls would be roughly 10 to 20 lines.
WOW! I am way off then. Actually, the assignment has (2) radio groups: one with (5) states and one with (5) capitals.
After designating the first radio button in each group as the default for each group, we were to enter the code to invoke the Click event for the two default radio buttons when the form is read into the computer's internal memory.