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

Dani AI

Generated

As pointed out, the most likely root cause is the event being used. ItemCheck fires before the ListView item's Checked state is updated; ItemChecked fires after. Reading an item's Checked property inside an ItemCheck handler therefore sees the old value, which makes add/remove logic appear inverted. Handling ItemChecked (or using e.NewValue inside ItemCheck) prevents that flip.

A compact, efficient pattern is to update the ListBox only for the changed item instead of scanning everything each time:

Private Sub ListView1_ItemChecked(sender As Object, e As ItemCheckedEventArgs) _
    Handles ListView1.ItemChecked

    If e.Item.Checked Then
        If Not ListBox1.Items.Contains(e.Item.Text) Then
            ListBox1.Items.Add(e.Item.Text)
        End If
    Else
        ListBox1.Items.Remove(e.Item.Text)
    End If
End Sub

Notes and production tips:

  • If rebuilding the whole ListBox, iterate ListView1.CheckedItems rather than all items for slightly clearer code and fewer checks.
  • Removing by display text can be wrong when two candidates share the same name. Store a unique identifier in ListViewItem.Tag (or bind a small object with Id/Name to the ListBox) and remove by ID instead of by text.
  • When changing checks programmatically, temporarily suppress the handler (or use a boolean flag) to avoid recursive or unexpected updates. Use BeginUpdate / EndUpdate on the ListBox when making many changes to prevent flicker.
  • For an e-voting app, keep a separate data model of selected candidate IDs (do not rely solely on UI state) and validate/persist selections server-side before finalizing a vote.

Recommended Answers

All 4 Replies

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.