使用vb.net计算复选框值 [英] count checkbox value using vb.net

查看:165
本文介绍了使用vb.net计算复选框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在开发一个使用vb.net windows应用程序的应用程序

因为我有几个douts,





i我的表格中有4个复选框



当我勾选前2个复选框时我需要在消息框中显示,就像你选择了2个复选框

以及

当我勾选4复选框时我需要在消息框中显示,就像你选择了4复选框一样。



这是我真正想要的但是在我的代码下面它不工作

所以请找出我的代码并让我知道我错误的地方,





Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles CheckedListBox1.SelectedIndexChanged

If CheckedListBox1.CheckedItems.Count< ;> 0然后

''如果是这样,循环检查所有选中的项目并打印结果。

Dim x As Integer

Dim s As String =

For x = 0 To CheckedListBox1.CheckedItems.Count - 1

s = s&Checked Item&(x + 1).ToString&=&CheckedListBox1 .CheckedItems(x).ToString&ControlChars.CrLf

下一个x

MessageBox.Show(s)

结束如果



结束次级



提前感谢...

解决方案

< blockquote>您可以尝试一个简单的代码:点击按钮,将出现消息框..





 Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button1.Click 
Dim i As Integer,k As Integer = 0
For i = 0 To CheckedListBox1.Items。 Count - 1
If(CheckedListBox1.Ge tItemChecked(i)= True)然后
k = k + 1
结束如果
下一个
MessageBox.Show(你选择了& k& items)
End Sub


这适用于VS 2012 Express:

 私有  Sub  Button1_Click(发件人作为 < span class =code-keyword> Object ,e  As  EventArgs) Handles  Button1。点击
Dim msg 作为 字符串 = 字符串 .Empty
对于 index As 整数 = 0 CheckedListBox1.CheckedItems.Count - 1
msg + = 项目& (index + 1 )。ToString& ,名称为& CheckedListBox1.CheckedItems(index).ToString& vbCrLf
下一步
MessageBox.Show(msg, Test Application,MessageBoxButtons.OK,MessageBoxIcon.Information,MessageBoxDefaultButton.Button1)
End Sub



虽然你可能会更好地使用Checkeditems集合上的ForEach循环,但它应该枚举。



别忘了CheckedItems系列包括Checked或Indeterminate案例,你可能会也可能不会想要不确定的案例。



Mike


am developing one application using vb.net windows application
in that i have few douts,
that is,

i have 4 checkbox in my form

when i tick first 2 checkbox i need to display in the message box like you have selected 2 checkbox
as well ,
when i tick 4 checkbox i need to display in the message box like you have selected 4 checkbox.

this is what i actually want but in my code below its not working
so please find out my code and let me know where i did mistake,


Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged
If CheckedListBox1.CheckedItems.Count <> 0 Then
'' If so, loop through all checked items and print results.
Dim x As Integer
Dim s As String = ""
For x = 0 To CheckedListBox1.CheckedItems.Count - 1
s = s & "Checked Item " & (x + 1).ToString & " = " & CheckedListBox1.CheckedItems(x).ToString & ControlChars.CrLf
Next x
MessageBox.Show(s)
End If

End Sub

thanks in advance...

解决方案

You can try a simple code: On button click, the messagebox will appear..


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer, k As Integer = 0
        For i = 0 To CheckedListBox1.Items.Count - 1
            If (CheckedListBox1.GetItemChecked(i) = True) Then
                k = k + 1
            End If
        Next
        MessageBox.Show("You have selected " & k & " items")
    End Sub


This works in VS 2012 Express:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim msg As String = String.Empty
    For index As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
        msg += "Item " & (index + 1).ToString & ", with name " & CheckedListBox1.CheckedItems(index).ToString & vbCrLf
    Next
    MessageBox.Show(msg, "Test Application", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
End Sub


Though you''d probably bet better using a ForEach loop on the Checkeditems collection, it should enumerate.

Don''t forget that the CheckedItems collection includes both Checked or Indeterminate cases, you may or may not want the indeterminate ones.

Mike


这篇关于使用vb.net计算复选框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆