我想编写代码,当单击复选框时,会显示隐藏的组框,然后执行简单的计算。 这是我的代码:
Public Class frmMenu
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim Burgers, Fries, Drinks, sum As Double
If chkBurgers.Checked Then
grpBurgers.Visible = True
If radRegular.Checked Then
Burgers = 4.19
ElseIf radWcheese.Checked Then
Burgers = 4.79
ElseIf radWbacon.Checked Then
Burgers = 4.79
ElseIf radWbcheese.Checked Then
Burgers = 5.39
End If
End If
If chkFries.Checked Then
grpFries.Visible = True
If radSmall.Checked Then
Fries = 2.39
ElseIf radMedium.Checked Then
Fries = 3.09
ElseIf radLarge.Checked Then
Fries = 4.99
End If
End If
If chkDrinks.Checked Then
grpDrinks.Visible = True
If radSoda.Checked Then
Drinks = 1.69
ElseIf radBottled.Checked Then
Drinks = 1.49
End If
End If
sum = Burgers + Fries + Drinks
txtCost.Text = sum.ToString("C")
End Sub
End Class
我用
If chkBurgers.Checked Then
grpBurgers.Visible = True
显示隐藏的组框,但它在VB 2012
中无效,我的代码出了什么问题?对我来说似乎很好。
答案 0 :(得分:2)
问题是你在按下按钮时管理可见性,而应该在检查复选框时完成。我会按照以下步骤进行;
Public Class frmMenu
Private Burgers, Fries, Drinks, sum As Double
Private Sub chkBurgers_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBurgers.CheckedChanged
grpBurgers.Visible = chkBurgers.Checked
If chkBurgers.Checked then
Select case True
Case radRegular.Checked
Burgers = 4.19
Case radWcheese.Checked
Burgers = 4.79
Case radWbacon.Checked
Burgers = 4.79
Case radWbcheese.Checked
Burgers = 5.39
Case else
Burgers = 0.00
End Case
End If
End Sub
Private Sub chkFries_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFries.CheckedChanged
grpFries.Visible = chkFries.Checked
If chkFries.Checked then
Select case True
Case radSmall.Checked
Fries = 2.39
Case radMedium.Checked
Fries = 3.09
Case radLarge.Checked
Fries = 4.99
Case else
Fries = 0.00
End Case
End If
End Sub
Private Sub chkDrinks_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDrinks.CheckedChanged
grpDrinks.Visible = chkDrinks.Checked
If chkDrinks.Checked then
Select case True
Case radSoda.Checked
Drinks = 1.69
Case radBottled.Checked
Drinks = 1.49
Case else
Fries = 0.00
End Case
End If
End Sub
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
sum = Burgers + Fries + Drinks
txtCost.Text = sum.ToString("C")
End Sub
End Class