ListView with CheckBox cell : ListView « GUI « VB.Net Tutorial






ListView with CheckBox cell
'Visual Basic.Net JingCai Programming 100 Examples
'Author: Yong Zhang
'Publisher: Water Publisher China
'ISBN: 750841156


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Imports System.Runtime.InteropServices
Imports System.IO

public class FileListView
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Dim nIndex As Integer = 0
    Private Sub CreateMyListView(ByVal dirpath As String)
        Dim hImgSmall As IntPtr
        Dim hImgLarge As IntPtr
        Dim shinfo As FileInfoClass.SHFILEINFO = New FileInfoClass.SHFILEINFO()
        ListView1.Clear()
        ImageListSmall.Images.Clear()
        ImageListLarge.Images.Clear()
        nIndex = 0
        ListView1.Columns.Add("File Name", 200, HorizontalAlignment.Left)
        ListView1.Columns.Add("Size", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Date", 100, HorizontalAlignment.Left)
        ListView1.Columns.Add("Attribute", 100, HorizontalAlignment.Center)

        ListView1.LargeImageList = imageListLarge
        ListView1.SmallImageList = imageListSmall
        ListView1.View = View.Details
        ListView1.LabelEdit = True
        ListView1.AllowColumnReorder = True
        ListView1.CheckBoxes = True
        ListView1.FullRowSelect = True
        ListView1.GridLines = True
        ListView1.Sorting = SortOrder.Ascending
        Dim FilesInDir As String() = Directory.GetFiles(dirpath, "*.*")
        Dim SFile As String
        Dim item1 As New ListViewItem("", 0)
        item1.SubItems.Add("1")
        item1.SubItems.Add("2")
        item1.SubItems.Add("3")
        Dim ctdate As Date
        Dim fAttr As FileAttribute
        For Each SFile In FilesInDir
            ctdate = IO.File.GetCreationTime(SFile)
            fAttr = IO.File.GetAttributes(SFile)
            shinfo.szDisplayName = New String(Chr(0), 260)
            shinfo.szTypeName = New String(Chr(0), 80)
            hImgSmall = FileInfoClass.SHGetFileInfo(SFile, 0, shinfo, _
                                    Marshal.SizeOf(shinfo), _
                                    FileInfoClass.SHGFI_ICON Or FileInfoClass.SHGFI_SMALLICON)
            ImageListSmall.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon))       'Add icon to smallimageList.
            hImgLarge = FileInfoClass.SHGetFileInfo(SFile, 0, shinfo, _
                                            Marshal.SizeOf(shinfo), _
                                            FileInfoClass.SHGFI_ICON Or FileInfoClass.SHGFI_LARGEICON)
            ImageListLarge.Images.Add(System.Drawing.Icon.FromHandle(shinfo.hIcon))       'Add icon to LargeimageList.
            ListView1.Items.Add(New ListViewItem(New String() {SFile, CStr(FileLen(SFile)), ctdate.ToString, fAttr.ToString}, nIndex))
            nIndex = nIndex + 1
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim disks As String() = Directory.GetLogicalDrives
        Dim Sdisk As String
        ComboBox1.Items.Clear()
        For Each Sdisk In disks
            ComboBox1.Items.Add(Sdisk)
        Next
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        CreateMyListView(ComboBox1.Text)
    End Sub
End Class
Public Class FileInfoClass
    Public Structure SHFILEINFO
        Public hIcon As IntPtr            
        Public iIcon As Integer           
        Public dwAttributes As Integer    
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
        Public szDisplayName As String
        <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
        Public szTypeName As String
    End Structure
    Public Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
            (ByVal pszPath As String, _
             ByVal dwFileAttributes As Integer, _
             ByRef psfi As SHFILEINFO, _
             ByVal cbFileInfo As Integer, _
             ByVal uFlags As Integer) As IntPtr
    Public Const SHGFI_ICON = &H100
    Public Const SHGFI_SMALLICON = &H1
    Public Const SHGFI_LARGEICON = &H0    ' Large icon
End Class

<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)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.ComboBox1 = New System.Windows.Forms.ComboBox
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.ImageListSmall = New System.Windows.Forms.ImageList(Me.components)
        Me.ImageListLarge = New System.Windows.Forms.ImageList(Me.components)
        Me.SuspendLayout()
        '
        'ComboBox1
        '
        Me.ComboBox1.FormattingEnabled = True
        Me.ComboBox1.Location = New System.Drawing.Point(12, 3)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(413, 20)
        Me.ComboBox1.TabIndex = 0
        '
        'ListView1
        '
        Me.ListView1.Location = New System.Drawing.Point(12, 29)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(413, 207)
        Me.ListView1.TabIndex = 1
        Me.ListView1.UseCompatibleStateImageBehavior = False
        '
        'ImageListSmall
        '
        Me.ImageListSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.ImageListSmall.ImageSize = New System.Drawing.Size(16, 16)
        Me.ImageListSmall.TransparentColor = System.Drawing.Color.Transparent
        '
        'ImageListLarge
        '
        Me.ImageListLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.ImageListLarge.ImageSize = New System.Drawing.Size(16, 16)
        Me.ImageListLarge.TransparentColor = System.Drawing.Color.Transparent
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(437, 246)
        Me.Controls.Add(Me.ListView1)
        Me.Controls.Add(Me.ComboBox1)
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents ImageListSmall As System.Windows.Forms.ImageList
    Friend WithEvents ImageListLarge As System.Windows.Forms.ImageList

End Class








14.15.ListView
14.15.1.ListView DemoListView Demo
14.15.2.Creating List Views in CodeCreating List Views in Code
14.15.3.Add columns and rows to a ListViewAdd columns and rows to a ListView
14.15.4.Change ListView state: LargeIcon, SmallIcon, List and DetailChange ListView state: LargeIcon, SmallIcon, List and Detail
14.15.5.Add and delete ListView ItemAdd and delete ListView Item
14.15.6.ListView Item Check eventListView Item Check event
14.15.7.Displaying directories and their contents in ListViewDisplaying directories and their contents in ListView
14.15.8.ListView ItemActivate eventListView ItemActivate event
14.15.9.ListView with CheckBox cellListView with CheckBox cell
14.15.10.Add file name to ListViewAdd file name to ListView
14.15.11.Runtime ListViewRuntime ListView