0

我基本上想要一份调查文件,用户可以在其中选择“总是”和“从不”等评级。他们通过点击单选按钮来选择评级。评分被加权并平均为总分,就像 GPA 一样,即总是值 3,有点值 2,每个问题都应该相加然后取平均值。以下是我针对一个问题的代码的一部分。

选择按钮后如何取最后的值并对所有问题取平均值?

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged

    If RadioButton1.Checked = True Then

        RadioButton3.Checked = False
        RadioButton2.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        num1 = 2
    End If

End Sub

Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
    If RadioButton2.Checked = True Then

        RadioButton3.Checked = False
        RadioButton1.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        num1 = 3
    End If
End Sub

Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
    If RadioButton3.Checked = True Then

        RadioButton1.Checked = False
        RadioButton2.Checked = False
        RadioButton4.Checked = False
        RadioButton5.Checked = False
        num1 = 1
    End If
End Sub

Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
    If RadioButton4.Checked = True Then

        RadioButton3.Checked = False
        RadioButton2.Checked = False
        RadioButton1.Checked = False
        RadioButton5.Checked = False
        num1 = 0
    End If
End Sub

Private Sub RadioButton5_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton5.CheckedChanged


    If RadioButton5.Checked = True Then

        RadioButton3.Checked = False
        RadioButton2.Checked = False
        RadioButton4.Checked = False
        RadioButton1.Checked = False
        num1 = 0
    End If
End Sub
4

2 回答 2

1

首先,您可以将所有这些 RadioButton 事件处理程序合并到一个处理程序中。此外,您不必“取消选中”组中的其他 RadioButtons,因为这将自动为您完成。最后,创建一个单独的方法来计算评分并更新表单,然后在处理程序的底部调用该方法。

这可能看起来像这样:

Private num1 As Integer

Private Sub set1_CheckedChanged(sender As Object, e As EventArgs) Handles _
        RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, _
        RadioButton4.CheckedChanged, RadioButton5.CheckedChanged

    If RadioButton1.Checked Then
        num1 = 2
    ElseIf RadioButton2.Checked Then
        num1 = 3
    ElseIf RadioButton3.Checked Then
        num1 = 1
    ElseIf RadioButton4.Checked Then
        num1 = 0
    ElseIf RadioButton5.Checked Then
        num1 = 0
    End If

    UpdateRating()
End Sub

Private Sub UpdateRating()
    ' ... compute the rating using "num1", "num2", "num3", etc ...
    ' ... then update the GUI with the new value ...
End Sub

请注意,所有五个 RadioButtons 都列在“Handles”关键字之后,使它们都触发相同的处理程序。您可以对每组 RadioButtons 执行类似的操作...只需确保每组 RadioButtons 都位于其OWN容器中,例如 GroupBox 或 Panel。RadioButton 与它自己容器中的所有其他 RadioButtons 是互斥的。

于 2013-06-13T00:49:44.377 回答
0

我不太确定你的情况..但你可以试试这个

声明将计算所有结果的 var ..

Dim num1 as Integer 
Dim num2 as Integer '--> for other group radiobuttons
Dim num3 as Integer '--> for other group radiobuttons

Dim nTotal as Integer
Dim nAvg as Integer

每个checkchanged中执行此操作

Private Sub RadioButton_CheckedChanged(..) Handles RadioButton1_CheckedChanged, RadioButton2_CheckedChanged,  .....

  'codes
  countit()

End Sub

Sub CountIt()
   nTotal = num1 + num2 + num3
   nAvg = (num1 + num2 + num3) / 3
End Sub
于 2013-06-13T00:44:49.180 回答