从GroupBox(单选按钮)计算

时间:2013-11-03 00:48:59

标签: vb.net

我是新手,我很困惑为什么当点击计算按钮时,总计是关闭的。任何指导或指出错误将不胜感激。

Public Class Form1

Dim IntegerValue1 As Double
Dim IntegerValue2 As Double


Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
    If RadioGlazed.Checked = True Then
        IntegerValue1 = 0.65
    ElseIf RadioSugar.Checked = True Then
        IntegerValue1 = 0.65
    ElseIf RadioChocolate.Checked = True Then
        IntegerValue1 = 0.85
    ElseIf RadioFilled.Checked = True Then
        IntegerValue1 = 1.0
    End If
End Sub

Private Sub GroupBox2_Enter(sender As Object, e As EventArgs) Handles GroupBox2.Enter
    If RadioNone.Checked = True Then
        IntegerValue2 = 0.0
    ElseIf RadioRegular.Checked = True Then
        IntegerValue2 = 1.8
    ElseIf RadioCapp.Checked = True Then
        IntegerValue2 = 2.5
    End If
End Sub


Private Sub ButtonCalc_Click(sender As Object, e As EventArgs) Handles ButtonCalc.Click

    SubTotal.Text = (IntegerValue1) + (IntegerValue2)

End Sub

1 个答案:

答案 0 :(得分:0)

关联值并捕获它的更简洁方法是将值(.65,.85等)分配给每个RB的.Tag属性,然后在单击时捕获它。这允许你完全取消SELECT CASE或IF / ELSEIF:

Private Sub RBGroup1_CheckedChanged(ByVal sender As System.Object, _
         ByVal e As System.EventArgs) Handles RadioGlazed.CheckedChanged, _
         RadioSugar.CheckChanged       
         ' add for each RB in the group

   IntegerValue1 = CType(sender, RadioButton).Tag
   ' may only need:
   'IntegerValue1 =sender.Tag

End Sub

一个处理程序允许1个事件处理它们。修改组2中其中一个RB的单击以对它们执行相同操作(这个用于IntegerValue2)。现在,当用户选择时,设置该值。确保删除旧的/额外的事件处理程序,并确保选择每个组中的一个RB开始,以便在表单加载时设置变量。如果您添加更多甜甜圈/饼干等,请务必添加新的处理程序及其TAG值。