9 Replies - 2844 Views - Last Post: 28 June 2010 - 02:59 PM Rate Topic: -----

#1 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Change font color in lv

Posted 09 June 2010 - 05:13 PM

Hi, can some provide some information how to change the following lv font color base on the values?

For example:
If the values are negative in red color, positive in green.

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDbl(reader("Close").ToString) - (reader("Open").ToString))


Also, I am having a problem on rounding the decimal place value on the above code.

For example:
If the result comes out xx.xx, then the my lv shows the value as xx.xxxxxxxxx. Actually I would like it to display only 2 decimal places.

Thank you for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: Change font color in lv

#2 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 590
  • Joined: 04-May 09

Re: Change font color in lv

Posted 09 June 2010 - 11:52 PM

Hi,

For your first question you can try something like this:

ListViewItem1.Font = New Font(ListViewItem1.Font, _ 
   ListViewItem1.Font.Style Or FontStyle.Bold)


I couldn't test it.
Was This Post Helpful? 0
  • +
  • -

#3 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: Change font color in lv

Posted 27 June 2010 - 03:47 AM

Thanks Luc001, but that will change the whole column's values font color.

Do While (reader.Read())
ListView1.Items.Add(reader("Name").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDate(reader("Date")).ToString("MM/dd/yyyy"))
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(reader("Open").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(reader("Close").ToString)
ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Math.Round(CDbl(reader("Close").ToString) - (reader("Open").ToString), 3))
Loop



From the above codes, when the reader reads the "Close" - "Open" if the value returns in negative, then the font color is red; if the value returns in positive then the font color is green.

Any pointer here is great.

Thank you!
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1385
  • View blog
  • Posts: 4,461
  • Joined: 25-September 09

Re: Change font color in lv

Posted 27 June 2010 - 06:52 AM

One way I've done it is to immediately go back through and colorize based on the subitems values.

        For Each itm As ListViewItem In ListView1.Items
            itm.UseItemStyleForSubItems = False
            if convert.toint16(itm.subitems(3).text) < 0 then
              itm.SubItems(3).ForeColor = Color.Red
            end if
        Next

by putting this immediately after your loop in your code.

To do it during the addition of the subitem it's easiest to do by building the listviewitem and then adding it to the listview. You will also have to set .UseItemStyleForSubItems to false for this to work. Here is an example where we are filling a listview with random data, but we want to color the 4th column red if the value is negative. After we have populated the text and subitems of the listviewitem then prior to adding the item to the listview, we check subitem(3)'s value and if it's negative, set the forecolor to red, otherwise, ensure that it is black. Once we've established the forecolor, we add the newly created listitem to listview1.
        For y As Integer = 1 To 30
            Dim x As Integer = CInt(Int(10 * Rnd(1)))
            Dim lv As New ListViewItem
            lv.UseItemStyleForSubItems = False
            lv.Text = "Hello #" & y
            lv.SubItems.Add(CStr(y))
            lv.SubItems.Add("sub" & y)
            lv.SubItems.Add(CStr(x - y))
            If x - y < 0 Then
                lv.SubItems(3).ForeColor = Color.Red
            Else
                lv.SubItems(3).ForeColor = Color.Black
            End If
            ListView1.Items.Add(lv)
        Next

This post has been edited by CharlieMay: 27 June 2010 - 10:37 AM

Was This Post Helpful? 0
  • +
  • -

#5 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: Change font color in lv

Posted 28 June 2010 - 10:34 AM

Thanks Charlie.

I have tried the first method to colorize the subitem but returned an error "A first chance exception of type 'System.FormatException' occurred in mscorlib.dll"
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1385
  • View blog
  • Posts: 4,461
  • Joined: 25-September 09

Re: Change font color in lv

Posted 28 June 2010 - 10:55 AM

Can you post your exact code where you're trying to do it. I can populate using the database objects like you're using to see if I've overlooked something.

Also, specify the line where you're getting the error.

UPDATE:
I created both methods using columns from the database and was able to get both examples to work so now I'm definitely interested in seeing your code. After making the necessary changes to get the calculation to work with Option Strict on, I really think the error has to do with your converting the values from the database column.tostring to double and then performing the calculation.

This post has been edited by CharlieMay: 28 June 2010 - 12:59 PM

Was This Post Helpful? 0
  • +
  • -

#7 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: Change font color in lv

Posted 28 June 2010 - 12:55 PM

Hi,

I just simply put the code right below the loop as below:

Do While (reader.Read())  

ListView1.Items.Add(reader("Name").ToString)  

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(CDate(reader("Date")).ToString("MM/dd/yyyy"))  

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(reader("Open").ToString)  

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(FormatNumber(reader("Close").ToString))  

ListView1.Items(ListView1.Items.Count - 1).SubItems.Add(Math.Round(CDbl(reader("Close").ToString) - (reader("Open").ToString), 3))  

Loop 


                For Each itm As ListViewItem In ListView1.Items
                    itm.UseItemStyleForSubItems = False
                    If Convert.ToInt16(itm.SubItems(4).Text) < 0 Then
                        itm.SubItems(4).ForeColor = Color.Red
                    End If
                Next



Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1385
  • View blog
  • Posts: 4,461
  • Joined: 25-September 09

Re: Change font color in lv

Posted 28 June 2010 - 01:03 PM

Try this
For Each itm As ListViewItem In ListView1.Items
  itm.UseItemStyleForSubItems = False
  If Convert.ToDouble(itm.SubItems(4).Text) < 0 Then
  itm.SubItems(4).ForeColor = Color.Red
  End If
Next


Change toInt16 to ToDouble my demo was using integers, you're using decimals

This post has been edited by CharlieMay: 28 June 2010 - 01:03 PM

Was This Post Helpful? 1
  • +
  • -

#9 vennesschan  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 112
  • Joined: 20-April 10

Re: Change font color in lv

Posted 28 June 2010 - 01:57 PM

Tks, it works. :clap:
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is online

  • This space intentionally left blank
  • member icon

Reputation: 1385
  • View blog
  • Posts: 4,461
  • Joined: 25-September 09

Re: Change font color in lv

Posted 28 June 2010 - 02:59 PM

View Postvennesschan, on 28 June 2010 - 02:57 PM, said:

Tks, it works. :clap:


Cool, glad I could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1