0

I am new to this and am confused as to why when the calculate button is clicked, the total is way off. Any guidance or pointing out of mistakes would be greatly appreciated.

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
4

1 回答 1

0

关联值并捕获它的一种更简洁的方法是将值(.65、.85 等)分配给.Tag每个 RB 的属性,然后在它们单击时捕获它。这允许您完全取消 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 值。

于 2013-11-03T01:24:39.593 回答