【问题标题】:Radio buttons and checkboxes in VBVB中的单选按钮和复选框
【发布时间】:2016-01-15 20:48:30
【问题描述】:

我遇到的问题是:

我有 3 个分组框。在第一个组框中,我有 3 个单选按钮选项,其值为 1,1.50,1,在第二个组框中,我还有 3 个单选按钮,其值几乎相同。在 groupbox 3 中,我有 7 个复选框,所有复选框都有值,还有一个小计框、税框和税款总计。

问题是当用户从每个组框中选择哪个变量将用于存储小计中的数据时。

Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click

    tax = 0.07
    If rd1.Checked = True Then
        pan &= "1.00"
    End If
    If rd2.Checked = True Then
        bread &= "1.50"
    End If
    If rd3.Checked = True Then
        softdrink &= "0.85"
    End If
    If checkbox1.Checked = True Then
        ingredients &= "0.50"
        subtotal = ?
        subtotalTxtBox.Text = subtotal
        taxTxtBox.Text = tax
        total = subtotal * tax
        totalTxtBox.Text = subtotal + total

【问题讨论】:

  • 你能试着澄清你的问题吗?我不知道你在问什么。
  • 好吧抱歉,我试图做的是从组框 1、2、3 中选择的任何单选按钮获取这些值并放入小计框中。比如 1+2+3 小计 = 6
  • 欢迎来到 Stack Overflow。我已经用你的帖子解决了一些英语问题。您是否发布了完整的代码?它的缺失和“结束”开始。
  • 对那个 Rohit Gupta 感到抱歉,是的,我在代码上留下了结尾

标签: vb.net


【解决方案1】:

下面是一个快速示例,展示了如何从每个 GroupBox 中获取选定的 RadioButton:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim rb1 As RadioButton = GetSelectedRadioButton(GroupBox1)
    Dim rb2 As RadioButton = GetSelectedRadioButton(GroupBox2)
    Dim rb3 As RadioButton = GetSelectedRadioButton(GroupBox3)

    If Not IsNothing(rb1) Then
        Debug.Print(rb1.Text)
    End If
    If Not IsNothing(rb2) Then
        Debug.Print(rb2.Text)
    End If
    If Not IsNothing(rb3) Then
        Debug.Print(rb3.Text)
    End If
End Sub

Private Function GetSelectedRadioButton(ByVal gb As GroupBox) As RadioButton
    Return gb.Controls.OfType(Of RadioButton).Where(Function(x)
                                                        Return x.Checked
                                                    End Function).FirstOrDefault
End Function

【讨论】:

  • 感谢您的回答,但我为什么要按 3 单选按钮并取 rb1 为 1.00 美元 + rb2 为 0.85 美元 + rb3 为 0.50 美元之类的数字,将该总和添加到小计框中。跨度>
  • 好的...我的代码向您展示了如何获取选择了哪些 3 个 RadioButton。现在只需将这些值加起来......是什么阻止你这样做?
猜你喜欢
  • 2018-01-06
  • 1970-01-01
  • 2011-10-18
  • 2023-04-06
  • 1970-01-01
  • 2017-09-20
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多