Add Selection Listener to the ListView
Imports System Imports System.Drawing Imports System.Data Imports System.IO Imports System.Collections Imports System.Windows.Forms Imports System.Drawing.Printing Public Class MainClass Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub End Class Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) 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. Friend WithEvents lstFavorites As System.Windows.Forms.ListView Friend WithEvents hdrName As System.Windows.Forms.ColumnHeader Friend WithEvents hdrUrl As System.Windows.Forms.ColumnHeader <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.lstFavorites = New System.Windows.Forms.ListView() Me.hdrName = New System.Windows.Forms.ColumnHeader() Me.hdrUrl = New System.Windows.Forms.ColumnHeader() Me.SuspendLayout() ' 'lstFavorites ' Me.lstFavorites.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.lstFavorites.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.hdrName, Me.hdrUrl}) Me.lstFavorites.Location = New System.Drawing.Point(8, 8) Me.lstFavorites.Name = "lstFavorites" Me.lstFavorites.Size = New System.Drawing.Size(504, 216) Me.lstFavorites.TabIndex = 2 Me.lstFavorites.View = System.Windows.Forms.View.Details ' 'hdrName ' Me.hdrName.Text = "Name" Me.hdrName.Width = 250 ' 'hdrUrl ' Me.hdrUrl.Text = "URL" Me.hdrUrl.Width = 250 ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(520, 261) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lstFavorites}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Protected Overrides Sub OnLoad(ByVal e As System.EventArgs) Dim item As New YourListViewItem("First Item") lstFavorites.Items.Add(item) If lstFavorites.Items.Count = 1 Then item.Selected = True End If End Sub Private Sub lstFavorites_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstFavorites.Click Dim item As YourListViewItem For Each item In lstFavorites.Items If item.Selected = True Then MessageBox.Show(item.ItemName) Exit For End If Next End Sub End Class Public Class YourListViewItem Inherits ListViewItem Public ItemName As String Public Sub New(ByVal i As String) ItemName = i Text = i SubItems.Add("Sub " & i) End Sub End Class