List View Colours

List View Colours

am 21.12.2007 01:12:48 von UR

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

Re: List View Colours

am 24.12.2007 13:49:37 von Jan Hyde

Mr. B 's wild thoughts were released on Fri, 21
Dec 2007 00:12:48 GMT bearing the following fruit:

>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.

I going to assume you mean listview although you've also
said listbox.

The problem seems to be you want to set the colour of the
listviewitem and not the listview. However you are setting
the colour of the *listview* and so that is what you get.

Try changing the colour of the *listviewitem* (which IIRC
has forecolor and backcolor properties)

J

>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

--
Jan Hyde

https://mvp.support.microsoft.com/profile/Jan.Hyde