【问题标题】:How to calculate how many checkboxes are checked in VB.Net如何计算在 VB.Net 中选中了多少个复选框
【发布时间】:2014-03-26 08:13:07
【问题描述】:

我有 3 个 asp.net 标准复选框控件和 1 个文本框。我检查了 1 和 3 复选框。在文本框中如何计算有多少复选框被选中?如果我选中 1,则文本框结果为 1。如果我选​​中 1,2,则文本框结果为 2。如果我选​​中所有复选框,则结果为 3

如何在 vb.net 中做到这一点?

【问题讨论】:

    标签: asp.net vb.net checkbox textbox


    【解决方案1】:
    textbox1.Text = IIf(checkbox1.Checked, 1, 0) + IIf(checkbox2.Checked, 1, 0) + IIf(checkbox3.Checked, 1, 0)
    

    【讨论】:

      【解决方案2】:

      我还没有检查过这个工作,但试试:

      dim count as integer

      计数 = 0

      For Each ctrl As Control In Page.Controls
      
          If TypeOf ctrl Is Checkbox Then
      
             count=count+1
      
          End If
      
      Next
      

      【讨论】:

        【解决方案3】:
        Dim count As Integer
        count = 0   
        If checkbox1.Checked  Then
            count = count + 1
        End If
        If checkbox2.Checked  Then
            count = count + 1
        End If
        If checkbox3.Checked  Then
            count = count + 1
        End If   
        textbox1.Text = count.ToString()

        如果你想检查多个控件的使用(我正在修改@Nick 代码):

        Dim count As Integer
        count = 0
        For Each ctrl As Control In Page.Controls
            If TypeOf ctrl Is CheckBox Then
               If CType(Control, CheckBox).Checked Then
                  count=count+1
               End If
            End If
        Next
        textbox1.Text = count.ToString()
        

        【讨论】:

        • 如果我使用无限的复选框,那么?这样做?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-10
        • 2013-09-05
        • 2011-06-15
        • 2018-01-08
        • 2015-07-30
        • 2013-01-21
        • 1970-01-01
        相关资源
        最近更新 更多