我正在尝试添加分配给特定复选框的值。我有3个复选框 - ansmach,calcu& copymach。我想我只计算了勾选一个复选框时计算的总数,但是当我有2个或3个勾选复选框时却没有计算。我想知道是否有人可以帮我查看我的代码。这是我的代码。
Private Sub chkansmach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkansmach.CheckedChanged
ansmach = 7500
calcu = 300
copymach = 37600
If chkansmach.Checked = True Then
officetotal = ansmach
ElseIf chkansmach.Checked = False Then
officetotal = 0
End If
total = comptotal + officetotal + peritotal
txttotal.Text = total
End Sub
Private Sub chkcalcu_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkcalcu.CheckedChanged
ansmach = 7500
calcu = 300
copymach = 37600
If chkcalcu.Checked = True Then
officetotal = calcu
ElseIf chkcalcu.Checked = False Then
officetotal = 0
End If
total = comptotal + officetotal + peritotal
txttotal.Text = total
End Sub
Private Sub chkcopymach_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkcopymach.CheckedChanged
ansmach = 7500
calcu = 300
copymach = 37600
If chkcopymach.Checked = True Then
officetotal = copymach
ElseIf chkcopymach.Checked = False Then
officetotal = 0
End If
total = comptotal + officetotal + peritotal
txttotal.Text = total
End Sub
答案 0 :(得分:0)
我会创建一个私有函数来检查所有3个复选框的状态,并像这样计算总数:
Private Sub HandleCheckBoxes()
Dim total = 0
If chkansmach.Checked Then total += 7500
If chkcalcu.Checked Then total += 300
If chkcopymach.Checked Then total += 37600
txttotal.Text = total.ToString()
End Sub
然后,所有3个事件处理程序都可以调用此函数。
请注意,也可以为所有3个复选框设置一个事件处理程序。只需使用
Private Sub checkboxes_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles chkansmach.CheckChanged, chkcalcu.CheckChanged, chkcopymach.CheckChanged
End Sub
答案 1 :(得分:0)
每个.NET控件都有一个Photos App Toolbar,其中包含一些特定于该控件的lagniappe数据。您可以做的是分配Tag属性(在设计器中或在Form Load事件中)。然后将所有CheckBox控件的CheckedChange事件绑定在一起,并在事件处理程序内部,如果选中相应的CheckBox,则通过Tag属性值递增 total 变量。
这是一个快速示例,我在Form Load事件中设置了Tag属性:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'Assign the Tag property of each CheckBox
chkansmach.Tag = 7500
chkcalcu.Tag = 300
chkcopymach.Tag = 37600
End Sub
'Bind all the CheckBox controls CheckChanged event together
Private Sub Checkbox_Changed(ByVal sender As Object, ByVal e As EventArgs) Handles chkansmach.CheckChanged, chkcalcu.CheckChanged, chkcopymach.CheckChanged
'Create a variable to store the total
Dim total As Integer = 0
'If a CheckBox is Checked, increment the total variable by the respective CheckBox's Tag property
If chkansmach.Checked Then
total += CInt(chkansmach.Tag)
End If
If chkcalcu.Checked Then
total += CInt(chkcalcu.Tag)
End If
If chkcopymach.Checked Then
total += CInt(chkcopymach.Tag)
End If
'Display the total
txttotal.Text = total.ToString()
End Sub