cromalily
March 28th, 2004, 04:49 PM
Hi,
If I have a column with values of type Double, and I would like to color their fonts based on their value, for example, the lowest value color to be black (RGB Value 0, 0, 0) and the largest value to be Red (RGB Value 255, 0, 0). How can I do that?
Thanks in advance for anyone could help!
Craig Gemmill
March 28th, 2004, 06:09 PM
I assume that when you say, "Column", you mean a listbox, so:
Add a Listbox to your form, and:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
Me.ListBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable
AddHandler Me.ListBox1.DrawItem, AddressOf Me.DrawItemHandler
AddHandler Me.ListBox1.MeasureItem, AddressOf Me.MeasureItemHandler
End Sub
Private Sub DrawItemHandler(ByVal sender As Object, ByVal e As DrawItemEventArgs)
Dim cMyColor As Color
'
'Required
'
e.DrawBackground()
e.DrawFocusRectangle()
'
'Get the color, based on the items index
'
Select Case e.Index
Case Is <= 255
cMyColor = Color.FromArgb(e.Index, 0, 0)
Case 256 To 511
cMyColor = Color.FromArgb(255, e.Index - 256, 0)
Case 512 To 767
cMyColor = Color.FromArgb(255, 255, e.Index - 512)
Case Else
'
'All white if the color is out of RGB range
'
cMyColor = Color.FromArgb(255, 255, 255)
End Select
'
'Draw colored text
'
e.Graphics.DrawString(CType(sender, ListBox).Items(e.Index).ToString, ListBox1.Font, New SolidBrush(cMyColor), e.Bounds.X, e.Bounds.Y)
End Sub 'DrawItemHandler
Private Sub MeasureItemHandler(ByVal sender As Object, ByVal e As MeasureItemEventArgs)
Dim fMyFont As Font = CType(sender, ListBox).Font
Dim sStringSize As SizeF = e.Graphics.MeasureString(Font.Name, Font)
'
'Set the height of the row, column doesn't matter as there is only uno
'
e.ItemHeight = CInt(sStringSize.Height)
End Sub 'MeasureItemHandler
cromalily
March 28th, 2004, 07:56 PM
Hi,
Thanks for your reply. But I did intend to say "column" instead of "listBox". I mean I have to color-code a column in a dataGrid according to their values. For example, the table contains temperatures of different month of several years, it looks something like this
mm/yy 1998 1999 2000
Jan 68.0 72.4 76.3
Feb 72.3 75.8 79.2
Mar 44.2 55.5 62.4
Apr 38.2 45.4 55.8
If the user choose to diplay data for year 1999, then values in that column need to be color coded. Black Color for 45.4 and Red for 72.4. And the rest should be distributed evenly.
Thank You!
Joanne