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

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » GUI » ListView 
14.15.9.ListView with CheckBox cell
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.EventArgsHandles 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.EventArgsHandles 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 IntegerAs 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(123)
        Me.ComboBox1.Name = "ComboBox1"
        Me.ComboBox1.Size = New System.Drawing.Size(41320)
        Me.ComboBox1.TabIndex = 0
        '
        'ListView1
        '
        Me.ListView1.Location = New System.Drawing.Point(1229)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(413207)
        Me.ListView1.TabIndex = 1
        Me.ListView1.UseCompatibleStateImageBehavior = False
        '
        'ImageListSmall
        '
        Me.ImageListSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.ImageListSmall.ImageSize = New System.Drawing.Size(1616)
        Me.ImageListSmall.TransparentColor = System.Drawing.Color.Transparent
        '
        'ImageListLarge
        '
        Me.ImageListLarge.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit
        Me.ImageListLarge.ImageSize = New System.Drawing.Size(1616)
        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(437246)
        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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.