Here's a code sample that creates a new type, ListItem2, with a string
property Table.
Note the code in listView1_DoubleClick to see that you can cast the ListItem
back into a ListItem2 and get your custom property Table.
NOTE: The following code should be pasted into a new VB form file. You WILL
NOT be able to look at the form in design mode because I've added a second
public class to the same file. You would normally break this into its own
separate .VB file.
The main changes are:
1. Added new class, ListItem2.
2. In the Form's Constructor (New), I added 3 ListItem2 objects
3. In the ListView1's DoubleClick, I cast the first selected item into a
ListItem2 object to display the Table property.
============================
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
Public Class ListItem2
Inherits ListItem
Private mTable As String
Public Sub New(ByVal [text] As String, ByVal subItems() As String, ByVal
table As String)
MyBase.New([text], subItems)
Me.mTable = table
End Sub
Public Property Table() As String
Get
Table = mTable
End Get
Set
mTable = value
End Set
End Property
End Class
Public Class Form1
Inherits System.WinForms.Form
Public Sub New()
MyBase.New()
Form1 = Me
'This call is required by the Win Form Designer.
InitializeComponent()
listView1.ListItems.Add(New ListItem2("Item1", New String() {"item1,
col2", "item1, col3"}, "table1"))
listView1.ListItems.Add(New ListItem2("Item2", New String() {"item2,
col2", "item2, col3"}, "table2"))
listView1.ListItems.Add(New ListItem2("Item3", New String() {"item3,
col2", "item3, col3"}, "table3"))
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
components.Dispose()
End Sub
#Region " Windows Form Designer generated code "
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
Private ColumnHeader3 As System.WinForms.ColumnHeader
Private ColumnHeader2 As System.WinForms.ColumnHeader
Private ColumnHeader1 As System.WinForms.ColumnHeader
Private WithEvents btnClose As System.WinForms.Button
Private WithEvents ListView1 As System.WinForms.ListView
Dim WithEvents Form1 As System.WinForms.Form
'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.
Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container()
Me.ListView1 = New System.WinForms.ListView()
Me.btnClose = New System.WinForms.Button()
Me.ColumnHeader3 = New System.WinForms.ColumnHeader()
Me.ColumnHeader2 = New System.WinForms.ColumnHeader()
Me.ColumnHeader1 = New System.WinForms.ColumnHeader()
ListView1.Location = New System.Drawing.Point(8, 8)
ListView1.Size = New System.Drawing.Size(280, 224)
ListView1.HideSelection = False
ListView1.FullRowSelect = True
ListView1.View = System.WinForms.View.Report
ListView1.ForeColor = System.Drawing.SystemColors.WindowText
ListView1.TabIndex = 0
ListView1.Anchor = System.WinForms.AnchorStyles.All
Dim a__1(3) As System.WinForms.ColumnHeader
a__1(0) = ColumnHeader1
a__1(1) = ColumnHeader2
a__1(2) = ColumnHeader3
ListView1.Columns.All = a__1
btnClose.Location = New System.Drawing.Point(208, 240)
btnClose.Size = New System.Drawing.Size(80, 24)
btnClose.TabIndex = 1
btnClose.Anchor = System.WinForms.AnchorStyles.BottomRight
btnClose.Text = "Close"
ColumnHeader3.Text = "ColumnHeader"
ColumnHeader3.TextAlign = System.WinForms.HorizontalAlignment.Left
ColumnHeader2.Text = "ColumnHeader"
ColumnHeader2.TextAlign = System.WinForms.HorizontalAlignment.Left
ColumnHeader1.Text = "ColumnHeader"
ColumnHeader1.TextAlign = System.WinForms.HorizontalAlignment.Left
Me.Text = "Form1"
Me.StartPosition = System.WinForms.FormStartPosition.CenterScreen
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.Controls.Add(btnClose)
Me.Controls.Add(ListView1)
End Sub
#End Region
Protected Sub btnClose_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Protected Sub listView1_DoubleClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles listView1.DoubleClick
Dim li As ListItem2 = CType(listView1.SelectedItems(0), ListItem2)
MessageBox.Show(li.Table)
End Sub
End Class