home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 17 controls / controlsdemo / listviewform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  11.5 KB  |  300 lines

  1. Imports System.IO
  2.  
  3. Public Class ListViewForm
  4.     Inherits System.Windows.Forms.Form
  5.  
  6. #Region " Windows Form Designer generated code "
  7.  
  8.     Public Sub New()
  9.         MyBase.New()
  10.  
  11.         'This call is required by the Windows Form Designer.
  12.         InitializeComponent()
  13.  
  14.         'Add any initialization after the InitializeComponent() call
  15.  
  16.     End Sub
  17.  
  18.     'Form overrides dispose to clean up the component list.
  19.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  20.         If disposing Then
  21.             If Not (components Is Nothing) Then
  22.                 components.Dispose()
  23.             End If
  24.         End If
  25.         MyBase.Dispose(disposing)
  26.     End Sub
  27.     Friend WithEvents ListView1 As System.Windows.Forms.ListView
  28.         Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
  29.     Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
  30.     Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
  31.     Friend WithEvents ColumnHeader4 As System.Windows.Forms.ColumnHeader
  32.     Friend WithEvents ColumnHeader5 As System.Windows.Forms.ColumnHeader
  33.     Friend WithEvents cboSort As System.Windows.Forms.ComboBox
  34.     Friend WithEvents btnSort As System.Windows.Forms.Button
  35.     Friend WithEvents btnList As System.Windows.Forms.Button
  36.     Friend WithEvents btnShowChecked As System.Windows.Forms.Button
  37.         
  38.     'Required by the Windows Form Designer
  39.     Private components As System.ComponentModel.Container
  40.  
  41.     'NOTE: The following procedure is required by the Windows Form Designer
  42.     'It can be modified using the Windows Form Designer.  
  43.     'Do not modify it using the code editor.
  44.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  45.         Me.btnSort = New System.Windows.Forms.Button()
  46.         Me.ListView1 = New System.Windows.Forms.ListView()
  47.         Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader()
  48.         Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader()
  49.         Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader()
  50.         Me.ColumnHeader4 = New System.Windows.Forms.ColumnHeader()
  51.         Me.ColumnHeader5 = New System.Windows.Forms.ColumnHeader()
  52.         Me.cboSort = New System.Windows.Forms.ComboBox()
  53.         Me.btnList = New System.Windows.Forms.Button()
  54.         Me.btnShowChecked = New System.Windows.Forms.Button()
  55.         Me.SuspendLayout()
  56.         '
  57.         'btnSort
  58.         '
  59.         Me.btnSort.Location = New System.Drawing.Point(272, 16)
  60.         Me.btnSort.Name = "btnSort"
  61.         Me.btnSort.Size = New System.Drawing.Size(72, 26)
  62.         Me.btnSort.TabIndex = 3
  63.         Me.btnSort.Text = "Sort"
  64.         '
  65.         'ListView1
  66.         '
  67.         Me.ListView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  68.                     Or System.Windows.Forms.AnchorStyles.Left) _
  69.                     Or System.Windows.Forms.AnchorStyles.Right)
  70.         Me.ListView1.CheckBoxes = True
  71.         Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4, Me.ColumnHeader5})
  72.         Me.ListView1.Location = New System.Drawing.Point(8, 56)
  73.         Me.ListView1.Name = "ListView1"
  74.         Me.ListView1.Size = New System.Drawing.Size(664, 264)
  75.         Me.ListView1.TabIndex = 0
  76.         Me.ListView1.View = System.Windows.Forms.View.Details
  77.         '
  78.         'ColumnHeader1
  79.         '
  80.         Me.ColumnHeader1.Text = "Name"
  81.         Me.ColumnHeader1.Width = 100
  82.         '
  83.         'ColumnHeader2
  84.         '
  85.         Me.ColumnHeader2.Text = "Size"
  86.         Me.ColumnHeader2.Width = 75
  87.         '
  88.         'ColumnHeader3
  89.         '
  90.         Me.ColumnHeader3.Text = "Date Created"
  91.         Me.ColumnHeader3.Width = 139
  92.         '
  93.         'ColumnHeader4
  94.         '
  95.         Me.ColumnHeader4.Text = "Date Modified"
  96.         Me.ColumnHeader4.Width = 154
  97.         '
  98.         'ColumnHeader5
  99.         '
  100.         Me.ColumnHeader5.Text = "Last Access"
  101.         Me.ColumnHeader5.Width = 177
  102.         '
  103.         'cboSort
  104.         '
  105.         Me.cboSort.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
  106.         Me.cboSort.DropDownWidth = 144
  107.         Me.cboSort.Items.AddRange(New Object() {"Name", "Size", "Date Created", "Date Modified", "Date Accessed"})
  108.         Me.cboSort.Location = New System.Drawing.Point(112, 16)
  109.         Me.cboSort.Name = "cboSort"
  110.         Me.cboSort.Size = New System.Drawing.Size(144, 26)
  111.         Me.cboSort.TabIndex = 2
  112.         '
  113.         'btnList
  114.         '
  115.         Me.btnList.Location = New System.Drawing.Point(16, 16)
  116.         Me.btnList.Name = "btnList"
  117.         Me.btnList.Size = New System.Drawing.Size(72, 26)
  118.         Me.btnList.TabIndex = 1
  119.         Me.btnList.Text = "List files"
  120.         '
  121.         'btnShowChecked
  122.         '
  123.         Me.btnShowChecked.Location = New System.Drawing.Point(360, 16)
  124.         Me.btnShowChecked.Name = "btnShowChecked"
  125.         Me.btnShowChecked.Size = New System.Drawing.Size(120, 26)
  126.         Me.btnShowChecked.TabIndex = 3
  127.         Me.btnShowChecked.Text = "Show Checked"
  128.         '
  129.         'ListViewForm
  130.         '
  131.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  132.         Me.ClientSize = New System.Drawing.Size(680, 325)
  133.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnShowChecked, Me.btnSort, Me.cboSort, Me.btnList, Me.ListView1})
  134.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  135.         Me.Name = "ListViewForm"
  136.         Me.Text = "ListView Demo"
  137.         Me.ResumeLayout(False)
  138.  
  139.     End Sub
  140.  
  141. #End Region
  142.  
  143.     Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
  144.         ' Get system directory
  145.         Dim path As String = System.Environment.SystemDirectory
  146.         ' show in caption
  147.         Me.Text = "Files in " & path
  148.  
  149.         ' show file names in that directory, using alternating colors
  150.         ShowFilesInListView(ListView1, System.Environment.SystemDirectory)
  151.         PaintAlternatingBackColor(ListView1, Color.White, Color.Cyan)
  152.         ' select the first one
  153.         cboSort.SelectedIndex = 0
  154.     End Sub
  155.  
  156.     ' A reusable routine that shows files in specified directory
  157.  
  158.     Sub ShowFilesInListView(ByVal lv As ListView, ByVal path As String)
  159.         ' Get the DirectoryInfo object corresponding to the path
  160.         Dim di As New DirectoryInfo(path)
  161.  
  162.         lv.BeginUpdate()
  163.         lv.Items.Clear()
  164.  
  165.         ' Get data about all the files.
  166.         Dim fi As FileInfo
  167.         For Each fi In di.GetFiles()
  168.             ' Add a new ListViewItem object, add to the Items collection
  169.             Dim item As New ListViewItem(fi.Name)
  170.  
  171.             lv.Items.Add(item)
  172.  
  173.             item.SubItems.Add(fi.Length)
  174.             item.SubItems.Add(fi.CreationTime)
  175.             item.SubItems.Add(fi.LastWriteTime)
  176.             item.SubItems.Add(fi.LastAccessTime)
  177.         Next
  178.         lv.EndUpdate()
  179.     End Sub
  180.  
  181.     ' A reusable routine that displays alternating color for row background
  182.  
  183.     Sub PaintAlternatingBackColor(ByVal lv As ListView, ByVal color1 As Color, ByVal color2 As Color)
  184.         Dim item As ListViewItem
  185.         Dim subitem As ListViewItem.ListViewSubItem
  186.  
  187.         For Each item In lv.Items
  188.             ' set the color for the Item.
  189.             If (item.Index Mod 2) = 0 Then
  190.                 item.BackColor = color1
  191.             Else
  192.                 item.BackColor = color2
  193.             End If
  194.  
  195.             ' Assign same color to all subitems.
  196.             For Each subitem In item.SubItems
  197.                 subitem.BackColor = item.BackColor
  198.             Next
  199.         Next
  200.     End Sub
  201.  
  202.     ' sorts the elements of a the ListView
  203.  
  204.     Private Sub btnSort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSort.Click
  205.  
  206.         ' Use the correct sort object.
  207.         Dim index As Integer = cboSort.SelectedIndex
  208.         Select Case index
  209.             Case 0
  210.                 ' Sort by name.
  211.                 ListView1.ListViewItemSorter = New CompareByName()
  212.             Case 1
  213.                 ' Sort by size.
  214.                 ListView1.ListViewItemSorter = New CompareBySize()
  215.             Case 2, 3, 4
  216.                 ' Sort by one of the date properties.
  217.                 ListView1.ListViewItemSorter = New CompareByDate(index)
  218.         End Select
  219.  
  220.         ' show we're busy doing the sort
  221.         Cursor.Current = Cursors.WaitCursor
  222.  
  223.         ' Now we can sort the ListView.
  224.         ListView1.Sort()
  225.         PaintAlternatingBackColor(ListView1, Color.White, Color.Cyan)
  226.         ' restore default cursor
  227.         Cursor.Current = Cursors.Default
  228.     End Sub
  229.  
  230.     ' an IComparer auxiliary class that helps to sort files by name
  231.  
  232.     Private Class CompareByName
  233.         Implements IComparer
  234.  
  235.         Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
  236.             ' cast the two arguments to ListViewItem objects
  237.             Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
  238.             Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
  239.             ' compare their text property
  240.             Return String.Compare(item1.Text, item2.Text)
  241.         End Function
  242.     End Class
  243.  
  244.     ' an IComparer auxiliary class that helps to sort files by their size
  245.  
  246.     Private Class CompareBySize
  247.         Implements IComparer
  248.  
  249.         Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
  250.             ' cast the two arguments to ListViewItem objects
  251.             Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
  252.             Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
  253.             ' compare their 2nd subitem (the size)
  254.             Return Math.Sign(CLng(item1.SubItems(1).Text) - CLng(item2.SubItems(1).Text))
  255.         End Function
  256.     End Class
  257.  
  258.     ' We can use one class for all date comparisons, 
  259.     ' just by passing a different subitem index.
  260.  
  261.     Private Class CompareByDate
  262.         Implements IComparer
  263.  
  264.         Dim Index As Integer
  265.  
  266.         ' items in the ListView will be sorted on the date column whose
  267.         ' index is passed to this constructor
  268.  
  269.         Sub New(ByVal subitemIndex As Integer)
  270.             Me.Index = subitemIndex
  271.         End Sub
  272.  
  273.         Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
  274.             ' cast the two arguments to ListViewItem objects
  275.             Dim item1 As ListViewItem = DirectCast(x, ListViewItem)
  276.             Dim item2 As ListViewItem = DirectCast(y, ListViewItem)
  277.             ' compare items on the specified datetime subitem
  278.             Return Date.Compare(Date.Parse(item1.SubItems(Index).Text), Date.Parse(item2.SubItems(Index).Text))
  279.         End Function
  280.     End Class
  281.  
  282.     ' Display a list of all checked (selected) ListView items
  283.  
  284.     Private Sub btnShowChecked_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnShowChecked.Click
  285.         Dim msg As String = ""
  286.         Dim item As ListViewItem
  287.  
  288.         For Each item In ListView1.CheckedItems
  289.             msg &= item.Text & ControlChars.CrLf
  290.         Next
  291.  
  292.         If msg.Length = 0 Then
  293.             msg = "No items have been checked"
  294.         Else
  295.             msg = "List of checked items" & ControlChars.CrLf & ControlChars.CrLf & msg
  296.         End If
  297.         MessageBox.Show(msg, "CheckedItems collection")
  298.     End Sub
  299. End Class
  300.