how to select all item in listview?

         For i = 0 To lvList.Items.Count - 1
            lvList.Items(i).Selected = True
        Next i
        PrintToolStripMenuItem.PerformClick()

Dani AI

Generated

Both and demonstrate the straightforward approach of iterating the items and marking them selected. That works, but a few practical points and alternatives are often missed and can save time when selection appears not to behave as expected.

  • Make sure multiple selection is allowed: set MultiSelect = True (otherwise only one item can be selected). See the official MultiSelect docs: ListView.MultiSelect.
  • When changing many items, wrap updates to avoid redraw flicker and speed things up: call BeginUpdate() before and EndUpdate() after making changes. Details: ListView.BeginUpdate.
  • If the selection is applied but not visible when the control loses focus, set HideSelection = False or call Focus() / EnsureVisible(...) on an item you want shown.
  • If the ListView runs in VirtualMode, you cannot treat the Items collection the same way. Use the virtual-list pattern (manage selection via indices/your data source) instead of iterating physical items: ListView.VirtualMode.
  • If the goal is a persistent “mark” rather than a UI selection (for example, select for processing or printing), consider using CheckBoxes and the item Checked state so the marks remain independent of focus or selection behavior: ListViewItem.Checked.

Also note that programmatic selection will raise selection events. If those handlers should not run during a bulk change, use a simple guard flag to ignore events while the operation is in progress. Finally, rather than simulating a menu click with PerformClick(), call the printing or processing routine directly after making the selection so timing and UI state are explicit.

One way is

For Each item As ListViewItem In ListView1.Items
    item.Selected = True
Next
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.