Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Sat Dec 15, 2018 11:12 am
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 1:35 pm 
Offline

Joined: Fri Jun 05, 2009 4:54 pm
Posts: 484
Location: London, England
Hi all,
Is it possible to change the text colour or a ListBox cell depending on its content?

For example - if any of the cells contain the word OUT OF SERVICE, it should be in red text.
I obviously have no way of telling which cell could contain the appropriate phrase so can't hard code it.

Please bear in mind, I am a relatively newbie to RealStudio, so need to be explained in idiot terms where and what code to enter (if possible).

Thank you all in advance for the help on this question.

_________________
RealStudio Professional Edition 2012 r2
Developing on OS X 10.8 (Mountain Lion)
Deploying on Windows XP / Vista / 7 / 8


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 3:40 pm 
Offline

Joined: Mon Aug 14, 2006 9:33 pm
Posts: 1774
The celltextPaint event of the Listbox is what you are looking for. Use .invalidatecell to force it to change.

_________________
Roger Clary
Class One Software
Educational Software for Lifelong Learning
http://www.classonesoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 4:18 pm 
Offline

Joined: Fri Jun 05, 2009 4:54 pm
Posts: 484
Location: London, England
Thank you very much but I still do not understand ??

Where do I place the code and do you have a basic example that I could study and learn from?

_________________
RealStudio Professional Edition 2012 r2
Developing on OS X 10.8 (Mountain Lion)
Deploying on Windows XP / Vista / 7 / 8


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 4:29 pm 
Offline

Joined: Mon Aug 14, 2006 9:33 pm
Posts: 1774
The code goes in the celltextPaint as I stated.

g.TextColor = &cFF000000

then call me.cell(0, 0).invalidate to force a change of color on the cells you need.
Try it, play around with it. See what happens

_________________
Roger Clary
Class One Software
Educational Software for Lifelong Learning
http://www.classonesoftware.com


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 4:53 pm 
Offline

Joined: Fri Jun 05, 2009 4:54 pm
Posts: 484
Location: London, England
Ok - I understand so far but that would surely make the text in ALL the cells red but I only want a certain phrase to become red.
The part I am having trouble with is how to determine what the text of the cell is - if that makes sense.

_________________
RealStudio Professional Edition 2012 r2
Developing on OS X 10.8 (Mountain Lion)
Deploying on Windows XP / Vista / 7 / 8


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 6:58 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
OK, idiot version here (joking!)

CellTextPaint gets called once for every visible cell. Each time it is called, you are passed the Row and Column numbers that are about to be drawn. This event gives you the opportunity to affect how the cell is drawn. In your case, you want to change the text color.

Given the Row number and Column number, you can get the contents of the cell with
me.Cell(Row,Column)

You can test the contents of the cell with
IF me.Cell(Row,Column) = "OUT OF SERVICE" Then

You are also passed a graphics object that represents the cell area being drawn. You should use that to do your own drawing, or change the drawing properties.
IF me.Cell(Row,Column) = "OUT OF SERVICE" Then
g.ForeColor = &cFF0000
End IF


HTH,
Tim


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Sun Oct 02, 2011 7:15 pm 
Offline

Joined: Fri Jun 05, 2009 4:54 pm
Posts: 484
Location: London, England
Wow - IT WORKED !

Thank you so much for the clear concise explanation and working example.

From that I have now learnt it immediately.

Thank you both.

_________________
RealStudio Professional Edition 2012 r2
Developing on OS X 10.8 (Mountain Lion)
Deploying on Windows XP / Vista / 7 / 8


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Wed Dec 28, 2011 12:24 pm 
Offline

Joined: Wed Dec 28, 2011 9:43 am
Posts: 2
Is it possible to base the color of the text inside of a listbox cell on a condition that is being evaluated outside of a Listbox.CellTextPaint event handler?

Real-world scenario: A library book is past its due date. It's expiration date should show up in a listbox cell as red. All other due dates in the listbox should remain black.

Pseudocode:
Fetch record from library books database.
If a book's due date is before today's date, color the due date red.


I can evaluate a condition inside of Listbox.CellTextPaint to determine text color, i.e.:
if me.Cell(row, column) = "cherry" then
g.ForeColor = rgb(255,0,0) // red
end if


But I can't figure out how to evaluate a condition outside of Listbox.CellTextPaint and then set the text color of a single cell based on that evaluation. Can this be done? If so, how?


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Wed Dec 28, 2011 12:37 pm 
Offline

Joined: Tue Aug 14, 2007 8:44 am
Posts: 583
I'm not sure why you are thinking about something outside of the listbox?

When you populate the list box, you can then change the cell colours by going through the visible rows and columns.

For example, library books - I suppose the cell will contain a date-like string - for example 2011-12-30. You could save the totalseconds in the celltag and then run something like if d.totalseconds< (whatever you due date is) THEN...

Would that work for you?


Last edited by Kyan on Thu Dec 29, 2011 2:34 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Wed Dec 28, 2011 5:10 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
In this case, I agree with Kyan that you should do the evaluation in the CellTextPaint event, since it's simple. However, if you have a more complex evaluation, you can set the CellTag to some value that you then test for in CellTextPaint.


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Wed Dec 28, 2011 11:15 pm 
Offline

Joined: Wed Dec 28, 2011 9:43 am
Posts: 2
The example was made as simple as possible to avoid confusion. The actual condition/evaluation that has me stymied is actually quite complicated. I can't imagine a way to place it in CellTextPaint.

Ideally, I'd like to fetch a record from the recordset, do the result of a Date value (i.e., DatabaseCursorField.DateValue.ShortDate) into a listbox cell, and then color the text in that cell red if the date is out of range. If I couldn't do it as the listbox got populated with Listbox.AddRow, then I hoped to flag the cell somehow for changing the color afterwards.

Kyan, my attempts at figuring out how to set the color of just one column as I populated the listbox failed. As I tried to diagnose my failures, it seemed like I kept running into mysterious (to me) behavior by the listbox. If you know how to do this, I'd be very much obliged if you would share the technique with me. Clearly that would be the most elegant way.

Here's a bare-bones example of what I tried . . .
(Objective: make the word "cherry" red without putting the test in CellTextPaint.)
Listbox1.AddRow("uncolored", "uncolored", "uncolored")
bColorize = true
Listbox1.AddRow("uncolored", "cherry", "uncolored")
bColorize = false
Listbox1.AddRow("uncolored", "uncolored", "uncolored")

. . . with the corresponding CellTextPaint . . .
if row = me.LastIndex then
if column = 1 then
if bColorize then
g.ForeColor = rgb(255,0,0) // red
end if
end if
end if


(Of course, to use the above code you'll need to set up a boolean property named bColorize and also a Listbox with three columns.)

If you happen to give the above code a whirl, I'd sure love to know why it almost works when you comment out the line that reads "bColorize = false" and the "if row = me.LastIndex" test. This results in every cell in column 1 colored red.

Also, thanks to both of you for the CellTag idea. It's been over four years since I've done any programming with RealBasic, and I had completely forgotten about this feature. I couldn't figure out how to set a boolean flag that a CellTextPaint event could properly catch on an individual cell-by-cell basis, but using CellTag should give me a means to achieve the same outcome.

Megathanks, gentlemen!


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Thu Dec 29, 2011 1:05 am 
Offline

Joined: Tue Aug 14, 2007 8:44 am
Posts: 583
Does this accompish what you want?

Sub FillList(lb as ListBox)
lb.DeleteAllRows
dim s() as string
s = Array("apple","orange","banana","grape","pear","plum")
dim i,j as Integer
dim r as New Random

for i = 0 to 54
lb.AddRow (s(r.InRange(0,5)),s(r.InRange(0,5)),s(r.InRange(0,5)))
for j = 0 to 2
if lb.Cell(i,j) = "apple" Then
lb.CellTag(i,j) = "RED"
elseif lb.Cell(i,j) = "grape" Then
lb.CellTag(i,j) = "PURPLE"
end
next
next


End Sub


Listbox CellTextPaint
Select Case me.CellTag(row,column)
Case "RED"
g.ForeColor = &cFF0000
g.Bold = True
Case "PURPLE"
g.ForeColor = &cFF33FF
g.Bold = True
else
end Select


Obviously, this is kind of repetitive, because you could write the CellTextPaint just to check the actual text of the cell in stead of the cellTag. But if you're really using dates, it'd make sense to have a date string in the cell and totalseconds in the celltag.

HTH!


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Thu Dec 29, 2011 2:10 am 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
You could put your boolean value in CellTag.
Listbox1.AddRow("uncolored", "uncolored", "uncolored")
Listbox1.AddRow("uncolored", "cherry", "uncolored")
Listbox1.CellTag(Listbox1.LastIndex, 1) = true
Listbox1.AddRow("uncolored", "uncolored", "uncolored")

And in CellTextPaint
if CellTag(row, column).BooleanValue then
g.ForeColor = rgb(255,0,0)
end

This would color only the cell that contains "cherry".

The problem with the code you posted is that the listbox is not painted immediately. There is some amount of discontinuity between setting the cells and when they are painted, so that kind of approach will seem to work sometimes, sorta, but not consistently. And then it would all be undone if the listbox had to be painted again, like when another window obscured it and then it uncovered. You must code in a way where timing doesn't matter. That's why I favor setting CellTag.


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Mon Jan 07, 2013 7:32 pm 
Offline

Joined: Mon Dec 07, 2009 7:30 pm
Posts: 121
Location: Europe/Italy
What if you have a method that performs a calculation where the result is displayed in a listbox cell...
If the calculation is negative then the text for that specific cell must be red else the text for that specific text must be black. Any ideas for best/easy way to do this?


Top
 Profile  
Reply with quote  
 Post subject: Re: ListBox Cell Text Colour
PostPosted: Mon Jan 07, 2013 8:40 pm 
Offline

Joined: Fri Jan 06, 2006 3:21 pm
Posts: 12388
Location: Portland, OR USA
In CellTextPaint, check the value of the cell and set ForeColor if necessary.
if column = X then   // whichever column contains the result
if CDbl(me.Cell(row, column)) < 0 then g.ForeColor = &cFF0000
end


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group