-
Feb 18th, 2003, 12:51 PM
#1
VB - Color a row in a ListView
This sub when called will color a ListView row.
VB Code:
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
-
Feb 21st, 2003, 01:45 PM
#2
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).
-
May 20th, 2005, 06:18 PM
#3
New Member
Re: VB - Color a row in a ListView
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.
-
May 20th, 2005, 07:21 PM
#4
Re: VB - Color a row in a ListView
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.
VB Code:
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
[HL="#FFFF80"]lv.ListItems(2).Selected = True
lv.ListItems(1).Selected = True[/HL]
Set itmX = Nothing
Set lvSI = Nothing
Exit Sub
ErrorRoutine:
MsgBox Err.Description
End Sub
-
Dec 17th, 2009, 03:42 PM
#5
Frenzied Member
Re: VB - Color a row in a ListView
Is it possible to colour the first Column and second column programatically?
If you find my thread helpful, please remember to rate me
-
Dec 17th, 2009, 04:16 PM
#6
Re: VB - Color a row in a ListView
This will do it.
Code:
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
Code:
ColorListviewColumns MyListView, 2, vbRed '
The column headers themselves won't be colored.
-
Dec 19th, 2009, 08:44 AM
#7
Re: VB - Color a row in a ListView
I made some changes to Marty 's code, so that it can make dual colors for the rows in ListView...
Code:
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:
Code:
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
......
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 7th, 2010, 11:52 AM
#8
Hyperactive Member
Re: VB - Color a row in a ListView
can someone give me a specific little tutorial on how to apply this please.
~[L!f3 !s @ll @ab0ut l3@rn!ng]~
~*D0nt Give up, h0pe is always present*~
-
Mar 8th, 2010, 05:28 AM
#9
Re: VB - Color a row in a ListView
Originally Posted by Condomx
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....
You have to pass your Listview's name and the color required....
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Mar 8th, 2010, 12:01 PM
#10
Hyperactive Member
Re: VB - Color a row in a ListView
I mean,what should i put to the load event when calling it.?
~[L!f3 !s @ll @ab0ut l3@rn!ng]~
~*D0nt Give up, h0pe is always present*~
-
Mar 8th, 2010, 12:41 PM
#11
Re: VB - Color a row in a ListView
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.
-
Mar 9th, 2010, 07:02 PM
#12
Hyperactive Member
Re: VB - Color a row in a ListView
oh ok,i though you need to call that public sub so that it will work.
~[L!f3 !s @ll @ab0ut l3@rn!ng]~
~*D0nt Give up, h0pe is always present*~
-
Jul 21st, 2010, 12:33 AM
#13
Lively Member
Re: VB - Color a row in a ListView
When a use clicks on a row or column the column or row must be HiLighted to any color.
is it possible ?
I belive in pray. I pray for you.
You may have happy, healthy, wealthy life.
-
Jul 21st, 2010, 08:06 AM
#14
Re: VB - Color a row in a ListView
Originally Posted by Eliminator2009
When a use clicks on a row or column the column or row must be HiLighted to any color.
is it possible ?
Try this:
Code:
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
.....
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 25th, 2011, 08:45 AM
#15
Fanatic Member
Re: VB - Color a row in a ListView
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
Code:
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
Code:
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
-
Apr 25th, 2011, 08:51 AM
#16
Re: VB - Color a row in a ListView
Since the newly added item will be appended as a new item at the end of second ListView, you could try something like this:
Code:
ColorListviewRow playlist.ListView1, playlist.ListView1.ListItems.Count, vbRed
I didn't tested it.
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
Apr 25th, 2011, 11:59 AM
#17
Fanatic Member
Re: VB - Color a row in a ListView
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!!
-
Apr 26th, 2011, 02:16 AM
#18
Re: VB - Color a row in a ListView
Can you show us the code ?
If my post was helpful to you, then express your gratitude using Rate this Post.
And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet
Social Group: VBForums - Developers from India
Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...
-
May 7th, 2011, 06:53 PM
#19
Banned
Re: VB - Color a row in a ListView
thats cool that ppl are learning simple things like adding color to a listview node
-
Aug 20th, 2011, 05:27 PM
#20
New Member
Re: VB - Color a row in a ListView
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..
-
Feb 8th, 2012, 05:25 AM
#21
Lively Member
Re: VB - Color a row in a ListView
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
-
Feb 8th, 2012, 05:38 AM
#22
Lively Member
Re: VB - Color a row in a ListView
Dear uzzeierrll
Can you post your code with database file,etc so that I could help you.
-
Feb 10th, 2012, 05:19 AM
#23
Lively Member
Re: VB - Color a row in a ListView
I tried Martin's code but in some rows it colours only 2-3 items, in some rows 3-4 items and in rows all items, whereas we requires each item to be coloured as required for the clicked row.
For everyone, I found a good solution at this site / thread:
http://forums.devx.com/showthread.php?t=169151
it colours each item of any clicked row in a listview to blue (you can change the colour). Thanks to Lennie (on that forum).
Last edited by Smartchap; Feb 10th, 2012 at 05:38 AM.
-
Sep 26th, 2012, 06:49 AM
#24
Fanatic Member
Re: VB - Color a row in a ListView
Hi Martin, Is it possible to make this code color subitems(3)
and if subitems(3) contains 'high' then it automatically color to red? as apose to the whole column being colored red?
Thanks
Jamie
-
Sep 26th, 2012, 09:38 AM
#25
Re: VB - Color a row in a ListView
Sure. Modify this part of my original code.
Code:
Set itmX = lv.ListItems(RowNbr)
'itmX.ForeColor = RowColor
'For intIndex = 1 To lv.ColumnHeaders.Count - 1
If itmX.ListSubItems(3).Text = "high" Then ' Im not sure that Text is correct but you get the idea
Set lvSI = itmX.ListSubItems(3)
lvSI.ForeColor = RowColor
DoEvents
End If
'Next
-
Dec 29th, 2014, 10:38 AM
#26
New Member
Re: VB - Color a row in a ListView
How can you change the font color of a row in your ListView when the row is selected? The blue highlight changes the font color back to white. Thanks!
-
Jan 4th, 2015, 04:02 AM
#27
Re: VB - Color a row in a ListView
It should be noted that this code can only be used with VB's Microsoft Windows Common Controls 6.0 (mscomctl instead of comctl32), which some people don't use for various reasons. Of course one advantage is that the coloring can be done without subclassing; using NM_CUSTOMDRAW would be required with 5.0.
(PS- A question is, will this color the background of a subitem image?)
If you had a subclassed 5.0 ListView, in its WndProc you'd do it like this:
Code:
Select Case uMsg
Case OCM_NOTIFY
Dim tNMH As NMHDR
CopyMemory tNMH, ByVal lParam, Len(tNMH)
Select Case tNMH.Code
Case NM_CUSTOMDRAW
Dim NMLVCD As NMLVCUSTOMDRAW
CopyMemory NMLVCD, ByVal lParam, Len(NMLVCD)
With NMLVCD.NMCD
Select Case .dwDrawStage
Case CDDS_PREPAINT
LVWndProc = CDRF_NOTIFYITEMDRAW
Exit Function
Case CDDS_ITEMPREPAINT
LVWndProc = CDRF_NOTIFYSUBITEMDRAW
Exit Function
Case CDDS_ITEMPREPAINT Or CDDS_SUBITEM
NMLVCD.ClrText = TEXTCOLOR
NMLVCD.ClrTextBk = BACKGROUNDCOLOR
CopyMemory ByVal lParam, NMLVCD, Len(NMLVCD)
LVWndProc = CDRF_NOTIFYSUBITEMDRAW
Exit Function
End Select
End With
End Select
End Select
This isn't the complete code to do it, it's just what you'd add if you were already using a 5.0 ListView AND had it subclassed.
If you wanted to make an item/subitem bold, you'd use SelectObject NMLVCD.NMCD.hDC, hFont_Bold (where hFont_Bold is a valid hFont; obviously you can change it to any font you want, Bold is just the most common use, also italics, different face; any font).
Last edited by fafalone; Jan 4th, 2015 at 04:11 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|