Click to See Complete Forum and Search --> : VB - Color a row in a ListView
MartinLiss
Feb 18th, 2003, 12:51 PM
This sub when called will color a ListView row.
Public Sub ColorListviewRow(lv As ListView, RowNbr As Long, RowColor As OLE_COLOR)
'***************************************************************************
'Purpose: Color a ListView Row
'Inputs : lv - The ListView
' RowNbr - The index of the row to be colored
' RowColor - The color to color it
'Outputs: None
'***************************************************************************
Dim itmX As ListItem
Dim lvSI As ListSubItem
Dim intIndex As Integer
On Error GoTo ErrorRoutine
Set itmX = lv.ListItems(RowNbr)
itmX.ForeColor = RowColor
For intIndex = 1 To lv.ColumnHeaders.Count - 1
Set lvSI = itmX.ListSubItems(intIndex)
lvSI.ForeColor = RowColor
Next
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
Usage: (To make the 5th row of a ListView Red)
ColorListviewRow MyListView, 5, vbRed
MartinLiss
Feb 21st, 2003, 01:45 PM
It has been pointed out that when using vbBlue as the color, only the first column gets colored! Why that should be I have no idea, but you can get around the problem by using 16711681 instead of vbBlue (which is 16711680).
DJ1234
May 20th, 2005, 07:18 PM
when I tried with this code, the first row is not getting colored. All other rows have the specific color mentioned in the code.
Please let me know how can I color the first row.
MartinLiss
May 20th, 2005, 08:21 PM
Welcome to VB Forums!
I assume what you mean is that the first column of the first row is getting colored, but not the 2nd, 3rd, etc columns of the first row. If that's the case then add the following because it seems that that row doesn't color until it gets focus.
Public Sub ColorListviewRow(lv As ListView, RowNbr As Long, RowColor As OLE_COLOR)
'***************************************************************************
'Purpose: Color a ListView Row
'Inputs : lv - The ListView
' RowNbr - The index of the row to be colored
' RowColor - The color to color it
'Outputs: None
'***************************************************************************
Dim itmX As ListItem
Dim lvSI As ListSubItem
Dim intIndex As Integer
On Error GoTo ErrorRoutine
Set itmX = lv.ListItems(RowNbr)
itmX.ForeColor = RowColor
For intIndex = 1 To lv.ColumnHeaders.Count - 1
Set lvSI = itmX.ListSubItems(intIndex)
lvSI.ForeColor = RowColor
DoEvents
Next
lv.ListItems(2).Selected = True
lv.ListItems(1).Selected = True
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
dinosaur_uk
Dec 17th, 2009, 03:42 PM
Is it possible to colour the first Column and second column programatically?
MartinLiss
Dec 17th, 2009, 04:16 PM
This will do it.
Public Sub ColorListviewColumns(lv As ListView, ColumnNumber As Long, ColumnColor As OLE_COLOR)
'***************************************************************************
'Purpose: Color a ListView Row
'Inputs : lv - The ListView
' ColumnNumber - The index of the row to be colored
' ColumnColor - The color to color it
'Outputs:
'***************************************************************************
Dim itmX As ListItem
Dim lvSI As ListSubItem
Dim intIndex As Integer
Dim intRow As Integer
On Error GoTo ErrorRoutine
For intRow = 1 To lv.ListItems.Count
Set itmX = lv.ListItems(intRow)
itmX.ForeColor = ColumnColor
Set lvSI = itmX.ListSubItems(ColumnNumber - 1)
lvSI.ForeColor = ColumnColor
Next
lv.ListItems(ColumnNumber).Selected = True
Set itmX = Nothing
Set lvSI = Nothing
lv.Refresh
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
Usage
ColorListviewColumns MyListView, 2, vbRed '
The column headers themselves won't be colored.
akhileshbc
Dec 19th, 2009, 08:44 AM
I made some changes to Marty 's code, so that it can make dual colors for the rows in ListView...
Public Sub ColorListviewRow(lv As ListView, Color1 As OLE_COLOR, Color2 As OLE_COLOR)
Dim itmX As ListItem
Dim lvSI As ListSubItem
Dim intIndex As Integer, i As Integer
On Error GoTo ErrorRoutine
For i = 1 To lv.ListItems.Count
If (i Mod 2) = 0 Then
Set itmX = lv.ListItems(i)
itmX.ForeColor = Color2
For intIndex = 1 To lv.ColumnHeaders.Count - 1
Set lvSI = itmX.ListSubItems(intIndex)
lvSI.ForeColor = Color2
DoEvents
Next
lv.ListItems(i).Selected = True
Else
Set itmX = lv.ListItems(i)
itmX.ForeColor = Color1
For intIndex = 1 To lv.ColumnHeaders.Count - 1
Set lvSI = itmX.ListSubItems(intIndex)
lvSI.ForeColor = Color1
DoEvents
Next
lv.ListItems(i).Selected = True
End If
Next
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
Example usage:
ColorListviewRow ListView1, vbGreen, vbRed
Example output:
abc def ghi jkl mno pqr stu vwx yz
abc def ghi jkl mno pqr stu vwx yz
abc def ghi jkl mno pqr stu vwx yz
abc def ghi jkl mno pqr stu vwx yz
abc def ghi jkl mno pqr stu vwx yz
......
Condomx
Mar 7th, 2010, 11:52 AM
can someone give me a specific little tutorial on how to apply this please.
akhileshbc
Mar 8th, 2010, 05:28 AM
can someone give me a specific little tutorial on how to apply this please.
Sample usage is provided at the end of each piece of code.... :wave:
You have to pass your Listview's name and the color required....
Condomx
Mar 8th, 2010, 12:01 PM
I mean,what should i put to the load event when calling it.?
MartinLiss
Mar 8th, 2010, 12:41 PM
Call what? You don't need to put anything into Form_Load. All you need is the code behind the "Color Row 5" button and the ColorListviewRow sub.
Condomx
Mar 9th, 2010, 07:02 PM
oh ok,i though you need to call that public sub so that it will work.
Eliminator2009
Jul 21st, 2010, 01:33 AM
When a use clicks on a row or column the column or row must be HiLighted to any color.
is it possible ?
akhileshbc
Jul 21st, 2010, 09:06 AM
When a use clicks on a row or column the column or row must be HiLighted to any color.
is it possible ?
Try this:
Option Explicit
Private Sub Form_Load()
'~~~ Adding some text for testing purpose (create two column headers in design time)
Dim i As Long
For i = 1 To 50
ListView1.ListItems.Add , , i
ListView1.ListItems(i).SubItems(1) = i
Next
End Sub
'~~~ When we click on it
Private Sub ListView1_Click()
ColorListviewRow ListView1, ListView1.SelectedItem.Index, vbRed '~~~ We are passing the Listview's name, selected item's index and the color to the sub
End Sub
'~~~ Sub for coloring
Public Sub ColorListviewRow(lv As ListView, sel_Item As Long, Color1 As OLE_COLOR)
Dim itmX As ListItem
Dim lvSI As ListSubItem
Dim intIndex As Integer, i As Integer
On Error GoTo ErrorRoutine
Set itmX = lv.ListItems(sel_Item)
itmX.ForeColor = Color1
For intIndex = 1 To lv.ColumnHeaders.Count - 1 '~~~ Coloring the full row with the selected color
Set lvSI = itmX.ListSubItems(intIndex)
lvSI.ForeColor = Color1
DoEvents
Next
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
.....:wave:
JamieWarren09
Apr 25th, 2011, 09:45 AM
Hey Everyone, Sorry to bring up a post in a topic that's not new!
But was wondering for some help!
I currently have 1 listview boxes and users can move the selected item from one listbox to the other.
i want it so when the user moves one item from listview1 to listview2 then the selected item they moved will be colored...
heres my code to move items from listview1 to listview2
Dim ListObj3 As ListItem 'Set listObj as a listitem
Set ListObj3 = playlist.ListView1.ListItems.Add(, , ListView3.SelectedItem.Text)
ListObj3.SubItems(3) = ListView3.SelectedItem.SubItems(3)
ListObj3.SubItems(4) = ListView3.SelectedItem.SubItems(4)
Which works fine!
How ever what can i change in this
ColorListviewRow playlist.ListView1, 5, vbRed
so that rather than the (5th line) be colored, its just the selected item they moved that gets colored red?
Thanks,
Jamie
akhileshbc
Apr 25th, 2011, 09:51 AM
Since the newly added item will be appended as a new item at the end of second ListView, you could try something like this:
ColorListviewRow playlist.ListView1, playlist.ListView1.ListItems.Count, vbRed
I didn't tested it.
:wave:
JamieWarren09
Apr 25th, 2011, 12:59 PM
Hey Mate,
That works!
Thanks,
However i am now getting a error saying ' Index out of bounds' i then press OK, and it still colors the thing as i wanted!
is there a way to trap that error, and ignore it?
i tried on error resume next,
how ever it still comes up!!
akhileshbc
Apr 26th, 2011, 03:16 AM
Can you show us the code ?
:wave:
lllllsp1d3rlllll
May 7th, 2011, 07:53 PM
thats cool that ppl are learning simple things like adding color to a listview node
uzzeierrll
Aug 20th, 2011, 06:27 PM
Hi Martin,
Can you help me? How to color each list items in my listview that connected to my database?
I have a code here that display in my listview the available room and not available room for the day. I want the available room to be in forecolor blue and not available room to be in red forecolor.
CODE:
Private Sub Form_Load()
Dim romarray() As Integer
Dim icounter As Integer
Dim romlength As Integer
icounter = 0
conn
RS.Open "SELECT * FROM room", CON, 3, 3
ReDim romarray(0 To RS.RecordCount - 1) As Integer
Do Until RS.EOF
romarray(icounter) = RS("roomNO")
icounter = icounter + 1
RS.MoveNext
Loop
RS.Close
CON.Close
For romlength = LBound(romarray) To UBound(romarray)
conn
RS.Open "SELECT * FROM room, availability WHERE availability.roomNO = '" & romarray(romlength) & "' AND availability.status_date = '" & Format(Date) & "' AND availability.roomNo=room.roomNo", CON, 3, 3
If RS.RecordCount = 1 Then
With viewall.ListItems.Add(, , romarray(romlength)) ------>not available room
.SubItems(1) = RS("room_status")
End With
ElseIf RS.RecordCount = 0 Then
viewall.ListItems.Add , , romarray(romlength) ------>available room
End If
RS.Close
CON.Close
Next romlength
End Sub
----------------------------------------------
SAMPLE OUTPUT:
101 Reserved
102 Available
103 Available
----------------------------------------------
Help me please..i really need it..thanks a lot..
Smartchap
Feb 8th, 2012, 05:25 AM
Hey Jammie
You are getting error "Index out of bounds" because you are using ListView1 in line
ColorListviewRow playlist.ListView1, playlist.ListView1.ListItems.Count, vbRed
Try using ListView2 as
ColorListviewRow playlist.ListView2, playlist.ListView2.ListItems.Count, vbRed
Smartchap
Feb 8th, 2012, 05:38 AM
Dear uzzeierrll
Can you post your code with database file,etc so that I could help you.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.