List View Cololurs

M

Mr. B

I've not programmed for a while in VB 2003... but I've decided to modlfiy a
Project I have.

Seems simple enough... I've a List Box... I fill it with about 30 lines of
data (3-4 columns).

What I want to do is to be able to List the ROWS of data either a RED or GREEN
colour depending upon an IF statement.

So... IF X =< myVariable then Row would be GREEN ELSE Row would be RED.

Orignally (before I tried to change the Colour) I had:

'Display All Results

LvItem = New ListViewItem
LvItem.Text = reqWire
LvItem.SubItems.Add(AcVolts)
LvItem.SubItems.Add(AcVoltLoss)
lvResults.Items.Add(LvItem)


Now I've (I get a colour... but all rows are the same):

'Display All Results

Dim lvs As New ListViewItem.ListViewSubItem
Dim LItem As ListViewItem
Dim nwClrA, nwClrB As Color
nwClrA = Color.PaleGreen ' Equal to or Less Than
nwClrB = Color.Salmon ' Greater Than

LvItem = New ListViewItem
LvItem.Text = reqWire
LvItem.SubItems.Add(AcVolts)
LvItem.SubItems.Add(AcVoltLoss)
If AcVoltLoss = maxVd Then
' Change Color of Items
lvs = LvItem.SubItems(0) ' Required Wire
lvs.BackColor = nwClrA
lvs = LvItem.SubItems(1) ' Volt Loss
lvs.BackColor = nwClrA
lvs = LvItem.SubItems(2) ' Volt Loss %
lvs.BackColor = nwClrA
Else
lvs = LvItem.SubItems(0) ' Required Wire
lvs.BackColor = nwClrB
lvs = LvItem.SubItems(1) ' Volt Loss
lvs.BackColor = nwClrB
lvs = LvItem.SubItems(2) ' Volt Loss %
lvs.BackColor = nwClrB
End If ' Update ListView
lvResults.Items.Add(LvItem)

So what am I doing wrong??

Thanks in advance!

BruceF
 
D

Dean Slindee

Here is a function that I use to shade the lines of a listview, depending on
the value in the first item:
Public Shared Sub ListViewPaintItemBackColor(ByRef frm As Form, _
ByRef lvw As ListView)
Dim itm As ListViewItem
Dim intSubItemCount As Integer = 0
Dim col As Integer = 0
For Each itm In lvw.Items
Select Case itm.Text
Case cContact
itm.BackColor = Color.LightYellow
Case cClient
itm.BackColor = Color.LavenderBlush
Case cPerson
itm.BackColor = Color.WhiteSmoke
Case Else
itm.BackColor = Color.Honeydew
End Select
intSubItemCount = itm.SubItems.Count
For col = 0 To intSubItemCount - 1
itm.SubItems(col).BackColor = itm.BackColor
'itm.SubItems(col).Font = New Font(itm.SubItems(col).Font,
FontStyle.Bold)
Next col
Call ListViewPaintItemStrikeout(lvw, itm, "DeathDate")
Next
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

List View Colours 1

Top