I'm working on an e-voting system right now. i'm having problems in passing a selection from list view to a list box. first is when an item is checked, it is pass in the list box.. but in my case the item is pass when the item is uncheck. second is when i try to uncheck the item, other items from my list view is pass as well even it is not selected..

this is my code so far...

For Each itemlist As ListViewItem In lstviewcandidate.Items

            If itemlist.Checked = True Then
                ListBox1.Items.Add(itemlist.Text)
            Else
                ListBox1.Items.Remove(itemlist.Text)
            End If
        Next

hope someone can help me on this. thanks in advance

Unless you need to keep certain data in ListBox1, clearing the ListBox before running the code should do the trick.

ListBox1.Items.Clear()
        For Each itemlist As ListViewItem In ListView1.Items
            If itemlist.Checked = True Then
                ListBox1.Items.Add(itemlist.Text)
            Else
                ListBox1.Items.Remove(itemlist.Text)
            End If
        Next

Unless you need to keep certain data in ListBox1, clearing the ListBox before running the code should do the trick.

ListBox1.Items.Clear()
        For Each itemlist As ListViewItem In ListView1.Items
            If itemlist.Checked = True Then
                ListBox1.Items.Add(itemlist.Text)
            Else
                ListBox1.Items.Remove(itemlist.Text)
            End If
        Next

thanks for the reply codeorder... it solve some problems like having multiple items in my list box. but i'm still having problems on checking the items in the list view. the items are pass every time the it is uncheck.. i hope you can help me.

Try the following code as a new project.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ListView1
            .Columns.Add("New Column", .Width - 21)
            .CheckBoxes = True : .FullRowSelect = True : .View = View.Details
            .Items.Add("item 1") : .Items.Add("item 2") : .Items.Add("item 3") : .Items.Add("item 4") : .Items.Add("item 5")
            .Items.Add("item 6") : .Items.Add("item 7") : .Items.Add("item 8") : .Items.Add("item 9") : .Items.Add("item 10")
        End With
    End Sub

    Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) _
                                                    Handles ListView1.ItemChecked
        ListBox1.Items.Clear()
        For Each itemListed As ListViewItem In ListView1.Items
            If itemListed.Checked = True Then
                ListBox1.Items.Add(itemListed.Text)
            Else
                ListBox1.Items.Remove(itemListed.Text)
            End If
        Next
    End Sub
End Class

The code only allows the Checked Items to be added to the Listbox, nothing else.
Even my previous reply, tested in a new project from a Button_Click event, returned the same result.

It could be that somewhere in your code you are adding all items to the Listbox, checked or unchecked.

Try the following code as a new project.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ListView1
            .Columns.Add("New Column", .Width - 21)
            .CheckBoxes = True : .FullRowSelect = True : .View = View.Details
            .Items.Add("item 1") : .Items.Add("item 2") : .Items.Add("item 3") : .Items.Add("item 4") : .Items.Add("item 5")
            .Items.Add("item 6") : .Items.Add("item 7") : .Items.Add("item 8") : .Items.Add("item 9") : .Items.Add("item 10")
        End With
    End Sub

    Private Sub ListView1_ItemChecked(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckedEventArgs) _
                                                    Handles ListView1.ItemChecked
        ListBox1.Items.Clear()
        For Each itemListed As ListViewItem In ListView1.Items
            If itemListed.Checked = True Then
                ListBox1.Items.Add(itemListed.Text)
            Else
                ListBox1.Items.Remove(itemListed.Text)
            End If
        Next
    End Sub
End Class

The code only allows the Checked Items to be added to the Listbox, nothing else.
Even my previous reply, tested in a new project from a Button_Click event, returned the same result.

It could be that somewhere in your code you are adding all items to the Listbox, checked or unchecked.

thanks codeorder.. It works already

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.