This class acts as a wrapper for the common dialog ListView control. It provides several useful enhancements such as full-row select and sorting when the user clicks
the column header when the ListView control is in report view. It allows you to specify additional styles such as check boxes and gridlines, as well as functions to set
and get the value of the check boxes.
Note: You must insert a ListView control onto a form in order to create a reference to the common controls library before the class will compile. Also, note that this code is not supported in the 64-bit version of Access 2010 or 2013 due to the use of the Common Controls of MSComCtl32.ocx.
|(Declarations)||Declarations||Declarations and private variables for the CListViewEnh class.|
|CheckBoxStyle||Property||Get the ListView control's CheckBox style.|
|Checked||Property||Get the value of the Checked property for the specified ListView item. The line items for the ListView control are numbered starting at zero. Set the indexed property to true or false to change the state of the check box on the selected row. Test the value of the indexed property to find out whether or not the row is currently checked.|
|FullRowSelect||Property||Determine whether the ListView control has the FullRowSelect style. By default when you click on an item in a ListView control when in report view, only the data in the first column is visibly selected. You can set a style attribute for the ListView control so that the entire row is selected. Set this property to true to enable the full row select style, or false to disable full row select.|
|GridLines||Property||Determine whether light gray GridLines are shown vertically and horizontally between the "cells" of the columns and rows of the ListView.|
|ListView||Property||Get a pointer to the private ListView control. You must set this property before using any of the other methods and properties of this class.|
|Redraw||Property||Get the value of the ListView control's Redraw property. To speed up loading items into the ListView control you can set the Redraw property to false. When all items are added, set Redraw back to true. This can reduce the flashing effect as items are added.|
|Class_Terminate||Terminate||Release resources used for this class.|
|AutoSizeColumns||Method||Sizes each column in the ListView control to fit the widest data in each column.|
|HandleClick||Method||Call this sub from the Onclick event of the assigned ListView control. For systems that do not have the updated version of the common controls which allow full-row selecting, this procedure causes the list item to be selected even if the user clicks on a column other than the first one. It tests the location of the click on the entire ListView control, determines which row was "clicked" and then selects that item.|
|HandleColumnClick||Method||Call this sub from the OnColumnClick event of the assigned ListView control. This causes the data in the ListView to be sorted in the order of the items in the column whose header was clicked.|
|HideColumn||Method||Hide the specified column by making its width 0.|
|GetCheckState||Private||Return value of check box when ListView is in report view and the ListView has the check box style attribute. This function is a private helper function used with the Checked property of the class. See "CheckBoxStyle" property.|
|SetCheckBoxStyle||Private||Set the ListView control style to include or remove the check box style. This function is a private helper function used with the CheckBoxStyle property of the class.|
|SetCheckState||Private||Set the check box on or off for the selected row in a ListView control. Control must be set to report view and have the check box style attribute. This function is a private helper function used with the Checked property of the class.|
|SetFullRowSelect||Private||Set the ListView control so that when a list item is selected, the entire row is selected. This function is a private helper function used with the FullRowSelect property of the class.|
|SetGridLines||Private||Set the ListView control style to include or remove the grid line style. This function is a private helper function used with the GridLines property of the class.|
|SetRedraw||Private||Suppress or allow redrawing of the ListView control. During long operations, such as sorting, resizing, or while loading the ListView control, turning off redrawing can improve appearance and performance. This function is a private helper function used with the Redraw property of the class.|
' Example of the CListViewEnh class ' This example assumes Access VBA as the host language ' To try this example, do the following: ' 1. Create a new form ' 2. Add a ListView control called 'lvwEmployees' ' Set the following properties ' View 3 - lvwReport ' 3. Add a command button called 'cmdCheckTwo' ' 4. Add a command button called 'cmdHideColumn' ' 5. Add a command button called 'cmdGetCheckedStates' ' 6. Add a command button called 'cmdAutosize' ' 7. Add 6 ColumnHeaders to the ListView control ' 8. Paste all the code from this example to the new form's module ' 9. Run the form ' Declarations section code Private mclsListViewEnh As CListViewEnh Private Sub cmdAutosize_Click() ' Autosize each column mclsListViewEnh.AutoSizeColumns End Sub Private Sub Form_Load() Dim li As ListItem ' Instantiate the module-level variable Set mclsListViewEnh = New CListViewEnh With mclsListViewEnh ' Assign an actual Listview to the ListView property of the class Set .ListView = lvwEmployees.Object ' Set style options .FullRowSelect = True .CheckBoxStyle = True .GridLines = True .Redraw = False End With ' Load ListView sample data Set li = lvwEmployees.ListItems.Add(, "E1", "1") With li .SubItems(1) = "Smith, John" .SubItems(2) = "225 West Road" .SubItems(3) = "Boston" .SubItems(4) = "55511" .SubItems(5) = "General Manager" End With Set li = lvwEmployees.ListItems.Add(, "E2", "2") With li .SubItems(1) = "Anderson, Maria" .SubItems(2) = "123 Pike" .SubItems(3) = "Seattle" .SubItems(4) = "15511" .SubItems(5) = "Sales" End With Set li = lvwEmployees.ListItems.Add(, "E3", "3") With li .SubItems(1) = "Arkuthnoth, Bill" .SubItems(2) = "Hoke Place #10" .SubItems(3) = "Salt Lake City" .SubItems(4) = "33511" .SubItems(5) = "Shipping" End With mclsListViewEnh.Redraw = True cmdCheckTwo.Caption = "Toggle Row 2 Check" cmdHideColumn.Caption = "Hide column 3" cmdGetCheckedStates.Caption = "Get Checked States" cmdAutosize.Caption = "Autosize Columns" End Sub Private Sub lvwEmployees_Click() ' Handle click events from the actual ListView control mclsListViewEnh.HandleClick End Sub Private Sub cmdCheckTwo_Click() ' Toggle the checked state programmatically mclsListViewEnh.Checked(1) = Not mclsListViewEnh.Checked(1) End Sub Private Sub cmdHideColumn_Click() ' Hide a selected column mclsListViewEnh.HideColumn 2 End Sub Private Sub cmdGetCheckedStates_Click() ' Retrieve the checked states for all rows Dim lngIndex As Long For lngIndex = 0 To lvwEmployees.ListItems.Count - 1 MsgBox lngIndex & " Checked: " & mclsListViewEnh.Checked(lngIndex) Next lngIndex End Sub Private Sub lvwEmployees_ColumnClick(ByVal ColumnHeader As Object) ' Handle ColumnClick events from the actual ListView control mclsListViewEnh.HandleColumnClick ColumnHeader End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP