VERSION 5.00
Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "mscomctl.ocx"
Begin VB.Form frmLvwBackColour
Caption = "Listview BackColor Without Subclassing."
ClientHeight = 6645
ClientLeft = 60
ClientTop = 450
ClientWidth = 11940
LinkTopic = "Form1"
ScaleHeight = 6645
ScaleWidth = 11940
StartUpPosition = 3 'Windows Default
Begin VB.PictureBox picBG
AutoRedraw = -1 'True
BackColor = &H80000009&
BorderStyle = 0 'None
Height = 375
Left = 9720
ScaleHeight = 375
ScaleWidth = 1335
TabIndex = 3
Top = 840
Visible = 0 'False
Width = 1335
End
Begin VB.CommandButton Command1
Caption = "&Close Me"
Height = 615
Left = 9480
TabIndex = 2
Top = 5355
Width = 1815
End
Begin VB.CheckBox Check1
Caption = "Make every other row coloured."
BeginProperty Font
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 1215
Left = 9480
TabIndex = 0
Top = 2400
Width = 2055
End
Begin MSComctlLib.ListView lvwBackColour
Height = 5295
Left = 480
TabIndex = 1
Top = 675
Width = 8835
_ExtentX = 15584
_ExtentY = 9340
View = 3
LabelWrap = -1 'True
HideSelection = -1 'True
Checkboxes = -1 'True
FullRowSelect = -1 'True
GridLines = -1 'True
_Version = 393217
ForeColor = -2147483640
BackColor = -2147483643
BorderStyle = 1
Appearance = 1
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "MS Sans Serif"
Size = 9.75
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
NumItems = 5
BeginProperty ColumnHeader(1) {BDD1F052-858B-11D1-B16A-00C0F0283628}
Text = "Column One"
Object.Width = 3528
EndProperty
BeginProperty ColumnHeader(2) {BDD1F052-858B-11D1-B16A-00C0F0283628}
SubItemIndex = 1
Text = "Column Two"
Object.Width = 2646
EndProperty
BeginProperty ColumnHeader(3) {BDD1F052-858B-11D1-B16A-00C0F0283628}
SubItemIndex = 2
Text = "Column Three"
Object.Width = 4410
EndProperty
BeginProperty ColumnHeader(4) {BDD1F052-858B-11D1-B16A-00C0F0283628}
SubItemIndex = 3
Text = "Column Four"
Object.Width = 4410
EndProperty
BeginProperty ColumnHeader(5) {BDD1F052-858B-11D1-B16A-00C0F0283628}
SubItemIndex = 4
Text = "Checked"
Object.Width = 0
EndProperty
End
End
Attribute VB_Name = "frmLvwBackColour"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Sub Check1_Click()
If Check1.Value = vbChecked Then
Dim i As Long
'\\ make sure there is data to work with
If lvwBackColour.ListItems.Count > 0 Then
'\\ set up pictuire box to colour rows
'\\ set the width of the picture box to the width of the listview
picBG.Width = lvwBackColour.Width
'\\ set the height of the picture box to the height
'\\ of the total number of rows in the listview
picBG.Height = lvwBackColour.ListItems(1).Height * (lvwBackColour.ListItems.Count)
picBG.ScaleHeight = lvwBackColour.ListItems.Count
'\\ set the ScaleWidth
picBG.ScaleWidth = 1
'\\ set the DrawWidth
picBG.DrawWidth = 1
'\\ clear the picture box from any previous colouring
picBG.Cls
'\\ loop through the rows to select every other row
For i = 1 To lvwBackColour.ListItems.Count
If (lvwBackColour.ListItems(i).Index Mod 2) = 0 Then
'\\ add a tick to the checkbox
lvwBackColour.ListItems(i).Checked = True
'\\ add the colour to the picturebox
'\\ See Here (http://msdn2.microsoft.com/en-us/library/aa230480(VS.60).aspx)
'\\ for information about the Line method
picBG.Line (0, i - 1)-(1, i), &H4000FF, BF
'\\ update column four caption
lvwBackColour.ListItems(i).SubItems(3) = "Hidden column value = 1"
Else
'\\ remove the tick from the checkbox
lvwBackColour.ListItems(i).Checked = False
'\\ reset backcolour to white
picBG.Line (0, i - 1)-(1, i), &HFFFFFF, BF
'\\ reset the Column Four caption
lvwBackColour.ListItems(i).SubItems(3) = "Hidden column value = 0"
End If
Next i
'\\ set the listview to use the picturebox image
lvwBackColour.Picture = picBG.Image
Else
'\\ No records in the listview
'\\ so clear the picture box of any previous colouring
picBG.Cls
'\\ set the listview to use the picturebox image
lvwBackColour.Picture = picBG.Image
'\\ reset the Column Four captions
For i = 1 To lvwBackColour.ListItems.Count
lvwBackColour.ListItems(i).SubItems(3) = "Hidden column value = 0"
Next i
End If
'\\change caption
Check1.Caption = "Remove colouring and empty checkboxes"
Else
'\\ Reset caption
Check1.Caption = "Make every other row coloured."
'\\ Clear the picture box
picBG.Cls
'\\ set the Listview backcolor to that of the cleared picturebox
lvwBackColour.Picture = picBG.Image
'\\ Remove the ticks in the Listview
For i = 1 To lvwBackColour.ListItems.Count
lvwBackColour.ListItems(i).Checked = False
'\\ reset the values in the hidden column back to zero for a fresh start
lvwBackColour.ListItems(i).SubItems(4) = 0
'\\ reset Column Four captions
lvwBackColour.ListItems(i).SubItems(3) = "Hidden column value = 0"
Next i
End If
End Sub
Private Sub Command1_Click()
'\\ close form
Unload Me
End Sub
Private Sub Form_Load()
Dim i As Long
Dim lvwItem As MSComctlLib.ListItem
'\\Load data into Listview
For i = 0 To 200
With lvwBackColour
Set lvwItem = lvwBackColour.ListItems.Add(, "k" & i, "Please Tick Me")
lvwItem.SubItems(1) = "My key is k" & i
lvwItem.SubItems(2) = "Is key k" & i & " checked yet?"
lvwItem.SubItems(3) = "Hidden column value = 0"
lvwItem.SubItems(4) = 0
End With
Next i
'\\remove initial highlighting
lvwBackColour.SelectedItem.Selected = False
End Sub
Private Sub lvwBackColour_ItemCheck(ByVal Item As MSComctlLib.ListItem)
Dim i As Long
'============================================================================
'
'See Check1_Click code for comments on the code below
'
'============================================================================
'\\ If checked/unchecked, change the value in the hidden column
If Item.Checked = True Then
lvwBackColour.ListItems(Item.Index).SubItems(4) = 1
Else
lvwBackColour.ListItems(Item.Index).SubItems(4) = 0
End If
'\\ make sure there is data to work with
If lvwBackColour.ListItems.Count > 0 Then
picBG.Width = lvwBackColour.Width
picBG.Height = lvwBackColour.ListItems(1).Height * (lvwBackColour.ListItems.Count)
picBG.ScaleHeight = lvwBackColour.ListItems.Count
picBG.ScaleWidth = 1
picBG.DrawWidth = 1
picBG.Cls
For i = 1 To lvwBackColour.ListItems.Count
If lvwBackColour.ListItems(i).SubItems(4) = 1 Then
lvwBackColour.ListItems(i).Checked = True
picBG.Line (0, i - 1)-(1, i), &H80FFFF, BF
lvwBackColour.ListItems(i).SubItems(3) = "Hidden column value = 1"
Else
lvwBackColour.ListItems(i).Checked = False
picBG.Line (0, i - 1)-(1, i), &HFFFFFF, BF
lvwBackColour.ListItems(i).SubItems(3) = "Hidden column value = 0"
End If
Next i
lvwBackColour.Picture = picBG.Image
Else
picBG.Cls
lvwBackColour.Picture = picBG.Image
End If
End Sub