Real value and not in a ListBox

Hi group, I have a listbox with a “real” value that occupies a certain cell, but in the PaintCellText property I change that value and set another one … now I need to read the modified value from the box, the one I display.
I tried with ListBoxAndamentoDitta(index).CellTextAt(row,3) and obviously it gives me the real value.
How do you do it?

PaintCellText is an event, not a property, that lets you redraw the contents of the cell.
So you are redrawing the Graphics object and not the value.

Inside that event the true value changes like this:
me.CellTextAt(row, column) = NewValue

So you can reread it the same way.

The real date is: 2025-01-02 . The Date viisualizated is: 02/01/2025
I tried with DateTime.FromString(ListBoxAndamentoDitta(index).CellTextAt(row,3), nil, new TimeZone(0)).ToString
and i read 2 gen 2025, 00:00:00
but it gives me the same REAL data not the displayed ones.
In particular, I write the date in SQL format (yyyy-mm-dd) but I see it as dd-mm-yy, I would need to read the date dd-mm-yy. I’m definitely reading this wrong.

OK, maybe I solved it, tell me if it’s ok:

I take the date from the cell and convert it to the format I’m interested in.

Var d As date = DateTime.FromString(ListBoxAndamentoDitta(index).CellTextAt(row,3), nil, new TimeZone(0))

TextFieldDataFattura.Text=d.ShortDate

Is that right?

Edit: sorry I read your code wrong, I thought you were using DateTime.ToString

TextFieldDataFattura.Text=DateTime.FromString(ListBoxAndamentoDitta(index).CellTextAt(row,3)).ToString(DateTime.FormatStyles.Short, DateTime.FormatStyles.None)

it’s Ok ?

No one can tell you because you overrode the default text drawing functionality. To get your modified value, you’ll have to access your modified code and you didn’t share that.

For the millionth time, store the date object in the row tag and use that. Stop stringifying and re-constructing dates. That’s slow and can lead to errors that shouldn’t happen to begin with.

4 Likes

I’m trying, but it’s not easy to understand how it works.

So basically, I’m interested in the date in the dd/mm/YYYY format, I’m trying to insert it as a true value in the table in the YYYY-mm-dd format, so I can sort the data very quickly using the default sorting. So I have a true value of 2025-01-02, but the value read in the listbox is 02/01/25 … now I’m interested in converting the real value or copying the value read that comes out in the 02/01/2025 format. I found this system:

var d As date = DateTime.FromString(ListBoxAndamentoDitta(index).CellTextAt(row,3), nil, new TimeZone(0))
TextFieldDataFattura.Text=d.ShortDate

d.ShortDate contains the value as I am interested: 02/01/2025.

What is wrong? Should I handle this differently? (aside from the fact that I figured it would be better to convert all dates to sql)

You shouldn’t be doing anything. The Xojo Prepared Statement should be turning the date into the correct format for the database.

You need to be using the DateTime object as the true value, not strings.

You are re-constructing a date from a string when you should be storing the original DateTime object in a Cell or RowTag. You can then use this tag later to format the date for display or re-insertion into the database.

Listbox.CellTagAt()

You’ll need to ensure your database is no longer using the localized date format from your previous threads, but once you do so you will never need DateTime.FromString.

Ok so I got to the point, I build a code that takes the date from all the tables from the database and converts them to SQL, and after that, I will store all the dates as SQL.

Store the date as a date in the RowTag or CellTag
Display it in the CellTextPaint in any way you wish.
Amend the value in the Tag
Display the amended value in any way that you wish.

Basically: stop trying to turn the displayed text back into a date. :slight_smile:
Get a real date, store a real date, amend a real date, display whatever you want.

Yes I decided to update all the database tables, so hopefully we won’t have any more problems.

P.S: While I’m at it, is there a way to check if a read date is in Sql format?

Regex?
But isnt it possible to make sure that the date in the database is stored that way?
Is everything a potential surprise?

I hopethat when you did this, you checked that each date was entirely valid (month>0 and month<13), day values good, etc etc.

You can always save both formats of the date in two different columns and hide the one you don’t want to display. Would that work for your project?

I shared a project that display the dates as Italian way (in Column1) and store the SQLDate in tag (Column 1).
I even - as a debug purpose - add a button to display the Tag from Colum 1 into Column 0 (nothing in Column 0 and I wanted to be sure of the tag values).

Apparently, it was useless / I wasted my time. Excepted that I learned things.

Something is wrong in this crazy world.

Yes, but in a future vision, I prefer to convert the data to SQL format once and for all and hope not to have any more problems of any kind.

No, you didn’t waste your time. I started studying and I understood how Me.CellTagAt(row1, column) works. For me these are all new concepts and sometimes it takes me a while to learn, doing a totally different job. However, following all your advice, I built a program with which, I take the data from the old database (which in some tables didn’t even have indexes) and I fix it … with indexes and dates in the Sql format YYYY-mm-dd HH:mm:ss. Now the next step is to modify the program and make the user see the date in the desired format :slight_smile:

P.S: When I open your program, it gives me errors and I can’t use it. However, it helped me study the code.


If I knew that earlier, I used an older version…

But, you can download the current version to load and print to pdf (as an example) the whole project (or print to paper)… So, you have the code.

Yes you can use it. Just don’t save it. Click Proceed and then it will open and you can examine the code.