98fatboyrider
asked on
There must be a more efficient way to varify if the correct radio button has been selected.
There must be a more efficient way of verifying if the correct radio button has been selected. Any suggestions?
Private Sub btbOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbOK.Click
Dim BoardForm As New frmQuestion
If RB1.Tag = m_CorrectAnswer Then
PlayerTotal = (m_sender * 200) + PlayerTotal
MessageBox.Show("Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("Sorry", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If RB2.Tag = m_CorrectAnswer Then
PlayerTotal = (m_sender * 200) + PlayerTotal
MessageBox.Show("That is Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("That is wrong!", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If RB3.Tag = m_CorrectAnswer Then
PlayerTotal = (m_sender * 200) + PlayerTotal
MessageBox.Show("That is Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("That is wrong!", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
If RB4.Tag = m_CorrectAnswer Then
PlayerTotal = (m_sender * 200) + PlayerTotal
MessageBox.Show("That is Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("That is wrong!", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Me.Close()
End Sub
ASKER
When using the code above, I'm getting syntax errors on all the "or" and "then". This is vb.net if that helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can use a custom control like this:
http://www.codeproject.com/KB/combobox/RadioListBoxDotNetVersion.aspx
It simultates a radio button collection and you can retrieve in one step with SelectedIndex
http://www.codeproject.com/KB/combobox/RadioListBoxDotNetVersion.aspx
It simultates a radio button collection and you can retrieve in one step with SelectedIndex
Pretty cool control jaime... =)
Another generic option is to iterate over the range of names you want to check and find the control on the form.
I wouldn't say this is more "efficient" than hard coding if the radiobuttons are static but I thought I would present it anyways since it has many uses:
(this version is for VB.Net 2005 or above)
Another generic option is to iterate over the range of names you want to check and find the control on the form.
I wouldn't say this is more "efficient" than hard coding if the radiobuttons are static but I thought I would present it anyways since it has many uses:
(this version is for VB.Net 2005 or above)
Private Sub btbOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbOK.Click
Dim correct As Boolean = False
For i As Integer = 1 To 5
Dim matches() As Control = Me.Controls.Find("RB" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is RadioButton Then
If matches(0).Tag = m_CorrectAnswer Then
correct = True
Exit For
End If
End If
Next
If correct Then
PlayerTotal = (m_sender * 200) + PlayerTotal
MessageBox.Show("Correct!", "Correct", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
MessageBox.Show("Sorry", "Wrong", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Me.Close()
End Sub
ASKER
Thanks for the help.
Second, you may want to consider putting the radio buttons in an option group. The benefits are that you can reduce the OK button to a single test:
If OptionGroup = m_correctAnswer
Msgbox "correct"
Else
Msgbox "wrong"
end if
Also, when one is selected the others will automatically go unselected so you don't have to manage that in code.
Open in new window