vb.net 使用基于被选中的复选框的选择语句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15649608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:59:51  来源:igfitidea点击:

Using a Select Statement Based on Check Box Being Checked

vb.netcheckboxvisual-studio-2012radio-buttonselect-case

提问by Dion Pezzimenti

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.

首先,我想说这是我正在做的家庭作业,但无法以这种方式工作。所以我必须使用 If 语句完成分配。目标是构建一个用户可以填写以提交订单的表单。有三个复选框(一个用于汉堡,一个用于薯条,另一个用于饮料)。如果您选中其中一个框,则组框将变得可见,其中包含用于进行选择的单选按钮。

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:

我想尝试根据复选框是否被选中来使用选择语句。然后为每个单选按钮使用不同的大小写。但是,如果我运行该程序并单击我提供的一个按钮,它将使用第一个可用案例填充我的 dblCost 变量。下面是我的代码。我真的很想了解我做错了什么,或者这是否不是解决这个问题的可行方法。下面是我想使用的代码:

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

结束班

采纳答案by ToddB

I think your understanding of select case is a little off. Take a look at thisand see if it helps.

我认为您对 select case 的理解有点偏离。看看这个,看看它是否有帮助。

回答by Derek Tomes

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.

这是一件小事,但它使代码更短。