How to check a checkbox in a ListView Details control

I have notice when you have a list view in details mode, if you click the checkbox beside the file or click the file link both interaction behave differently. If I click in the checkbox it throws an error with the code below because it does not pick up a count.

Dim index as interger=0
index=lvPartfiles.SelectedItems(0).Index

Open in new window




ListView DetailsListView Details
cmdolcetAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
Something quick and dirty:

Form1.vb -
Public Class Form1
	Dim IsLoading As Boolean = True
	Dim files As List(Of FileAction) = (From i In Enumerable.Range(1, 5) Select New FileAction With {.FilePath = $"C:\SomeFolder\SomeFile{i}", .Action = $"SomeAction{i}"}).ToList()
	Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
		ListView1.Columns.AddRange((From column In {"File Path", "Action"} Select New ColumnHeader With {.Name = column.Replace(" ", ""), .Text = column, .Width = 300}).ToArray())
		For Each action In files
			Dim item As ListViewItem = New ListViewItem({action.FilePath, action.Action}) With {.Tag = action}
			ListView1.Items.Add(item)
		Next
		IsLoading = False
	End Sub

	Private Sub OnItemChecked(sender As Object, e As ItemCheckedEventArgs) Handles ListView1.ItemChecked
		If Not IsLoading Then
			MessageBox.Show($"Checked Items:{Environment.NewLine}{String.Join(Environment.NewLine, (From item In CType(sender, ListView).CheckedItems.Cast(Of ListViewItem) Select $"{CType(item.Tag, FileAction)}"))}")
		End If
	End Sub
End Class

Class FileAction
	Public Property FilePath() As String
	Public Property Action() As String
	Public Overrides Function ToString() As String
		Return $"{{ FilePath: {FilePath}, Action: {Action} }}"
	End Function
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
	Inherits System.Windows.Forms.Form

	'Form overrides dispose to clean up the component list.
	<System.Diagnostics.DebuggerNonUserCode()> _
	Protected Overrides Sub Dispose(ByVal disposing As Boolean)
		Try
			If disposing AndAlso components IsNot Nothing Then
				components.Dispose()
			End If
		Finally
			MyBase.Dispose(disposing)
		End Try
	End Sub

	'Required by the Windows Form Designer
	Private components As System.ComponentModel.IContainer

	'NOTE: The following procedure is required by the Windows Form Designer
	'It can be modified using the Windows Form Designer.  
	'Do not modify it using the code editor.
	<System.Diagnostics.DebuggerStepThrough()> _
	Private Sub InitializeComponent()
		Me.ListView1 = New System.Windows.Forms.ListView()
		Me.SuspendLayout()
		'
		'ListView1
		'
		Me.ListView1.CheckBoxes = True
		Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
		Me.ListView1.Location = New System.Drawing.Point(0, 0)
		Me.ListView1.Name = "ListView1"
		Me.ListView1.Size = New System.Drawing.Size(800, 450)
		Me.ListView1.TabIndex = 0
		Me.ListView1.UseCompatibleStateImageBehavior = False
		Me.ListView1.View = System.Windows.Forms.View.Details
		'
		'Form1
		'
		Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
		Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		Me.ClientSize = New System.Drawing.Size(800, 450)
		Me.Controls.Add(Me.ListView1)
		Me.Name = "Form1"
		Me.Text = "Form1"
		Me.ResumeLayout(False)

	End Sub

	Friend WithEvents ListView1 As ListView
End Class

Open in new window

Produces the following output as you check and uncheck the items -Capture.PNG-saige-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.