Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

VS 2005
SQL Server 2005

One of the fields in a listview control is a calculated integer field.
Is there anyway that when the value shown is in the minus, the font colour changes?
Any help would be appreciated
Thanks
Posted

Try the following code:


VB
Private Sub Form_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Form.Load
    lvw.OwnerDraw = True
End Sub

Private Sub lvw_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles lvw.DrawColumnHeader
    e.DrawDefault = True
End Sub
Private Sub lvw_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles lvw.DrawItem
    Dim ColIndex As Integer = 1
    e.Item.UseItemStyleForSubItems = False
    Dim si As ListViewItem.ListViewSubItem = e.Item.SubItems(ColIndex)
    If Val(si.Text) < 0 Then
        si.ForeColor = Color.Red
    Else
        si.ForeColor = Color.Black
    End If
    e.DrawDefault = True
End Sub
Private Sub lvw_DrawSubItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewSubItemEventArgs) Handles lvw.DrawSubItem
    e.DrawDefault = True
End Sub


Change "lvw" to the name of your listview and change the ColIndex to the index of the value column.

If you need an example on usage, let me know. I can either send you an example solution or load one up on CodeProject.

Hope this helps.
 
Share this answer
 
v4
How about this[^] technique?
 
Share this answer
 
Comments
Member 7940373 11-Jul-11 6:47am    
Hi,
Thanks for the reply. I am new to VB so I dont quite follow. Either the whole row or the Integer field needs to be red when the value goes below 0. How could I achieve this/
Thanks
Dr.Walt Fair, PE 11-Jul-11 9:12am    
Whenever you set the value, if it is negative, set the FontColor for one or the all the Cells in the Row to Red, otherwise, set them to some other color. When you set the value, you should know which row it's going into, so just change the color of that row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900