Add Selection Listener to the ListView : ListView « GUI « VB.Net






Add Selection Listener to the ListView

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

           
         
  








Related examples in the same category

1.ListView Custom sortListView Custom sort
2.Build ListView at run time: update title column textBuild ListView at run time: update title column text
3.Construct ListView at run time: build structure and insert recordsConstruct ListView at run time: build structure and insert records
4.Fill XML data into ListView and add column to ListView at run timeFill XML data into ListView and add column to ListView at run time
5.Large Icon and small Icon for ListView
6.Add Customized Item to ListViewAdd Customized Item to ListView
7.Add Items to ListViewAdd Items to ListView
8.Displaying directories and their contents in ListViewDisplaying directories and their contents in ListView
9.Set ListView header and fill dataSet ListView header and fill data
10.Drag and Drop Explorer
11.ListView DataReader