stupid question about listbox colors

I’ve written a datebook app, but I’d like to have one row colored yellow if the date in column 0 matches the current date. I’m working in cellbackgroundpaint…seems logical to me, but I keep getting “outofbounds” errors when I run the app. Any help would be greatly appreciated.

Here’s what I trying:

dim d as New Date if me.cell(row,0) = d.AbbreviatedDate then g.ForeColor = &cFFFF80 g.FillRect(0, 0, g.Width, g.Height) end if

I realize this is likely a stupid request, but then I’m not very experienced, so please be patient with me. :wink:

I solved it.
Seems I needed:

if column = 0 then if row >= me.ListCount then exit else if me.cell(row,0) = d.AbbreviatedDate then g.ForeColor = &cFFFF80 g.FillRect(0, 0, g.Width, g.Height) end if end if end if

Thanks anyway, folks. :slight_smile:

    dim d as New Date
if row < me.ListCount then
    if me.cell(row,0) = d.AbbreviatedDate then
      g.ForeColor = &cFFFF80
      g.FillRect(0, 0, g.Width, g.Height)
    end if
  end if

In the help it says…

CellBackgroundPaint fires for every visible row in a ListBox, regardless of whether there is an actual row there or not. This enables you to implement the background paint event for the entire ListBox in a consistent way. For example, to do alternating row colors. Because of this, you need to check whether the current row is less than ListCount when accessing the row, for example with the Cell method.

so you need to put something like

if row < me.listcount then ... end if

around your code

Thanks guys, must have figured it out as you were typing your replies. I appreciate the help, nonetheless.

I check the last row for the word missing (all cases) and draw the Row background using a magenta flavour.

Nice as a recaller…

Same kind of code.

BTW: warning about the use of AbbreviatedDate. This may (or not) work on your computer but can gives various answers on someone’s other computer (especially for people not using USA dates…)

@ Emile: Thanks for the heads-up about the date. what format do you recommend?

You may want to look at Xojo.Core.Date which has the ToText method that lets you pick the locale and the format of the date.

See http://developer.xojo.com/xojo-core-date$ToText

Otherwise you can always create your own, stable way independent of region setting using something like :

Dim d As new Date dim months() as string = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") Msgbox months(d.Month-1)+" "+ str(d.Day)+", "+str(d.Year)

The thing is, how does the data gets in your listbox, and is it always in that format or can it be in another one ?

Here is the new framework way :

Using Xojo.Core Dim d As Date = Date.Now Dim t As Text = d.ToText(Locale.Current, Date.FormatStyles.Medium, Date.FormatStyles.None) msgbox t

The big problem in the code you marked as answer is here :

      if me.cell(row,0) = d.AbbreviatedDate then

It works only if the data in the listbox cell is identical as the abbreviateddate, for instance Jun 29,2015. If for any reason the format in that cell is slightly different, your code no longer works.

[quote=197611:@Michel Bujardet]You may want to look at Xojo.Core.Date which has the ToText method that lets you pick the locale and the format of the date.

See http://developer.xojo.com/xojo-core-date$ToText

Otherwise you can always create your own, stable way independent of region setting using something like :

Dim d As new Date dim months() as string = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") Msgbox months(d.Month-1)+" "+ str(d.Day)+", "+str(d.Year)

The thing is, how does the data gets in your listbox, and is it always in that format or can it be in another one ?[/quote]

Thanks for responding, Michel. You’ve given me something to learn.
My listbox gets the date/time input from my SQLite db, which in turn gets its data from Mike Cotrone’s open source Calendar / Time Picker, as chosen by the user. Thus, my date is always in the “Jun-29-2015” format, which matches the AbbreviatedDate format.

I’ll look into what you’ve pointed me to and see what my old brain can absorb. Thanks again.

[quote=197700:@Bill Dawson]Thanks for responding, Michel. You’ve given me something to learn.
My listbox gets the date/time input from my SQLite db, which in turn gets its data from Mike Cotrone’s open source Calendar / Time Picker, as chosen by the user. Thus, my date is always in the “Jun-29-2015” format, which matches the AbbreviatedDate format.

I’ll look into what you’ve pointed me to and see what my old brain can absorb. Thanks again.[/quote]

See, if indeed the cell date in in the format “Jun-29-2015” that is not what I get with AbbreviatedDate on Mac here, which is “Jun 29, 2015”. So the line where you test would not work on my machine. Then you should use this global module method so you compare with the same format :

Function Concise(extends dte as date) As string dim months() as string = Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec") Return months(dte.Month-1)+"-"+ str(dte.Day)+"-"+str(dte.Year) End Function

like so :

if me.cell(row,0) = d.Concise then

PS : Using your brain is the best way to keep it in shape. Cheers from a 64 yo youngster :wink:

I had not used my brain. Jun-29-2015 is sqlDate. So all you need is

if me.cell(row,0) = d.sqlDate then

Sorry…

[quote=197712:@Michel Bujardet]I had not used my brain. Jun-29-2015 is sqlDate. So all you need is

if me.cell(row,0) = d.sqlDate then

Sorry…[/quote]
I must be having a slow brain day…so I’ll still keep trying to get my head around what you posted before this…for future reference.
I also did not know that sqlDate was an option, so thanks for solving this problem in such a simple way. I see your name a lot around here, and I just want you to know that your contributions are very much appreciated. I’ve learned a lot from you over the past few months.

EDIT: actually. sqlDate doesn’t work…the cell background fails to show any color. So back I go to your previous instructions. :slight_smile:

[quote=197716:@Bill Dawson]I must be having a slow brain day…so I’ll still keep trying to get my head around what you posted before this…for future reference.
I also did not know that sqlDate was an option, so thanks for solving this problem in such a simple way. I see your name a lot around here, and I just want you to know that your contributions are very much appreciated. I’ve learned a lot from you over the past few months.

EDIT: actually. sqlDate doesn’t work…the cell background fails to show any color. So back I go to your previous instructions. :)[/quote]

Thank you for the kind words.

If you are sure the cell contains the Jun-29-2015 format, the Concise method I posted should work just fine. That said, I do not quite understand why it did not work with sqlDate which, here, shows just the same.

I suppose you place the code you posted in CellPaint ? Are you sure it runs ? Maybe you need an InvalidateCell() somewhere…

???
That is not an sqldate.

[quote=197725:@Tim Hare]???
That is not an sqldate.[/quote]

You are right. sqlDate is 2015-06-29. Sorry for the confusion.

[quote=197718:@Michel Bujardet]Thank you for the kind words.

If you are sure the cell contains the Jun-29-2015 format, the Concise method I posted should work just fine. That said, I do not quite understand why it did not work with sqlDate which, here, shows just the same.

I suppose you place the code you posted in CellPaint ? Are you sure it runs ? Maybe you need an InvalidateCell() somewhere…[/quote]
Yep. The code is in CellBackgroundPaint, and it does run with the desired effect. I’m implementing your code slice as suggested. I’m compiling only for windows, but safe is better than sorry, right? :wink:
Thanks again for all your help.

Michel, I get errors in the debugger when using your code as you describe:
“This item does not exist
if me.cell(row,0) = d.Concise”

and

“The extends modifier cannot be used on a class method
Function Concise(extends dte as date) As string”

Your function was correctly set up as a method…I don’t understand these errors???

move it to a module

Thanks, Axel.
Did as you instructed, but now my yellow background is gone.
Seems that “if me.cell(row,0)” does not equal “d.Concise”.

Near as I can figure, this should work. Any thoughts?