Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows VB.NET
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...
Posted 22-Mar-13 21:38pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
stellus at 23-Mar-13 5:56am
   
thanks a lot ispreet, the code which was send me that its working fine,
thank you..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 428
1 Maciej Los 249
2 BillWoodruff 199
3 /\jmot 180
4 Suraj Sahoo | Coding Passion 150
0 OriginalGriff 8,484
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,639
3 Maciej Los 5,159
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100