How to check a checkbox in a ListView Details control

cmdolcet
cmdolcet used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
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-

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial