如何计算在VB.Net中检查了多少个复选框

时间:2010-11-09 17:50:01

标签: asp.net vb.net checkbox textbox

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

如何在vb.net中执行此操作?

3 个答案:

答案 0 :(得分:0)

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()

答案 1 :(得分:0)

textbox1.Text = IIf(checkbox1.Checked, 1, 0) + IIf(checkbox2.Checked, 1, 0) + IIf(checkbox3.Checked, 1, 0)

答案 2 :(得分:0)

我没有检查过这项工作,但请尝试:

dim count as integer

count = 0

For Each ctrl As Control In Page.Controls

    If TypeOf ctrl Is Checkbox Then

       count=count+1

    End If

Next