I think your understanding of select case is a little off. Take a look at this and see if it helps.
Using a Select Statement Based on Check Box Being Checked
-
29-03-2022 - |
Вопрос
Firstly I would like to say this was a homework assignment I was working on, but couldn't get it to work this way. So I had to complete the assignment using If statements. The objective was to build essentially a form that a user could fill out to submit an order. There are three check boxes (one for burgers, one for fries, and another for drinks). If you check one of the boxes a group box will then become visible containing radio buttons to make the selection you want.
I wanted to try to use a select statement based on if the check box had been checked. Then use different cases for each of the radio buttons. However if I run the program and click the one button I supplied it will populate my dblCost variable with the first Case available. Below is my code. I just really want to understand what I was doing wrong or if this isn't a feasible way of approaching this problem. Below is the code I wanted to use:
Public Class frmRestaurantOrder
Private Sub CheckedChanged(sender As Object, e As EventArgs) Handles cbxBurgers.CheckedChanged, cbxFries.CheckedChanged, cbxDrinks.CheckedChanged
If (cbxBurgers.Checked) Then
gbxBurgers.Visible = True
Else
gbxBurgers.Visible = False
End If
If (cbxFries.Checked) Then
gbxFries.Visible = True
Else
gbxFries.Visible = False
End If
If (cbxDrinks.Checked) Then
gbxDrinks.Visible = True
Else
gbxDrinks.Visible = False
End If
End Sub
Private Sub btnCompute_Click(sender As Object, e As EventArgs) Handles btnCompute.Click
Dim dblCost As Double = 0
Select Case gbxBurgers.Visible = True
Case rbtRegularBurger.Checked
dblCost += 4.19
Case rbtCheeseBurger.Checked
dblCost += 4.79
Case rbtBaconBurger.Checked
dblCost += 4.79
Case rbtBaconCheeseBurger.Checked
dblCost += 5.39
Case Else
dblCost += 0
End Select
Select Case cbxFries.Checked = True
Case rbtSmallFries.Checked
dblCost += 1.29
Case rbtLargeFries.Checked
dblCost += 1.59
Case Else
dblCost += 0
End Select
Select Case cbxDrinks.Checked = True
Case rbtSoda.Checked
dblCost += 1.69
Case rbtWater.Checked
dblCost += 1.49
Case Else
dblCost += 0
End Select
txtCost.Text = FormatCurrency(dblCost)
End Sub
End Class
Решение
Другие советы
While it doesn't specifically address the question, you may also want to consider a shorter way to write your code:
Rather than:
If (cbxBurgers.Checked) Then
gbxBurgers.Visible = True
Else
gbxBurgers.Visible = False
End If
You can write:
gbxBurgers.Visible = cbxBurgers.Checked
It's a small thing, but it makes the code a lot shorter.