ListBox content text storage ?

This is not a coding question, but a design (logic ?) question.

The context:
This is a feature of a running project that use a ListBox to display data.
Today, the data comes either from a text file (.txt) or a csv (.csv) file. Both have in their first line the Headin strings (string that will be set in the ListBox Heading).

In another project, the .txt files comes from that project and I added the column widths in the .txt file second line.

I also added an automatic columns width function. I may add a way to set all column to a fixed value (pre-defined value).

I also added some goodies that are outside the scope of this question.

Now, I feel the need to set some column(s) alignments like a Size column (file or archive size) or a number column (Magazine issue # for example) or eventually the SQLDate Column centered.

The question is: where can I store these data ?

The only place I see is inside the file in one or more “Service Lines”, say:

Line 0: Version # of the file or (better ?) the # of service lines (used to know how to deal
with the data that follows) only one number,
Line 1: Heading Strings,
Line 2: Column Widths Strings (comma separated fixed values),
Line 3. Column Alignment String (also comma separated values),
Line 4: Any other improvement I will think at in the future…
*

What remains in the file is the data to load into the ListBox.

What is your feeling ?

  • I can add a special tag to flag the end of the “Service Lines” in the last line of the “Service Lines” block.

Hi Emile,

If you also need to store some “private” data for each row, column or cell it might be better to use XML.

With XML you can add tags to each column/row/cell to specify the CellType, the CellTag and other useful information.
Furthermore if some cell contain an EndOfline character, you can’t save it as is in the CSV file.

I belive the best way would be some sort of HTML as it is used to define a Table.

You could start with something like this, and add additional features for column widths, if the header is visible and so on:

[code]Function ExportToXML(extends List As Listbox) As String
Dim Lines() As String
Dim cell() As String
Dim row() As String
Dim i, j As Integer

Lines.Append “”

Lines.Append “


Lines.Append “”

//Cycling through each Header
For i = 0 to List.ColumnCount-1
Redim cell(-1)
cell.Append “<th”
If list.ColumnTag(i).StringValue <> “” then
cell.Append " data-tag=""" + list.ColumnTag(i).StringValue + “”""
End If
If list.ColumnType(i) <> list.TypeDefault then
cell.Append " data-type=""" + str(list.ColumnType(i)) + “”""
End If
cell.Append “>”
cell.Append list.Heading(i)
cell.Append “”

Lines.Append Join(cell, "")

Next

Lines.Append “


Lines.Append “”

//Cycling through each row
For i = 0 to List.ListCount-1
Redim row(-1)
row.Append “<tr”
If List.RowTag(i).StringValue <> “” then
row.Append " data-tag=""" + list.RowTag(i).StringValue + “”""
End If
row.Append “>”
Lines.Append Join(row, “”)

For j = 0 to List.ColumnCount-1
  Redim cell(-1)
  cell.Append "<td"
  If List.CellTag(i, j).StringValue <> "" then
    cell.Append " data-tag=""" + list.CellTag(i,j).StringValue + """"
  End If
  If List.CellType(i, j) <> list.TypeDefault or List.ColumnType(j) <> list.TypeDefault then
    cell.Append " data-type=""" + str(list.CellType(i,j)) + """"
  End If
  cell.Append ">"
  cell.Append List.Cell(i, j)
  cell.Append "</td>"
  Lines.Append Join(cell, "")
Next
Lines.Append "</tr>"

Next

Lines.Append “

Return Join(Lines, EndOfLine)
End Function
[/code]

Merci Jrmie.

That leads to the use of a dedicated file type to store (and read) the data in (from).

Talking about html tables, I saw earlier today (wikipedia) a table that sort the data if a click in the header is done strange / I do not take time to watch the html code.

That would be some javascript that is used on Wikipedia to sort a column.

I use such a javascript library every day: http://www.kryogenix.org/code/browser/sorttable/

Thanks.

Less than a month after…

I worked on a RAW xml export method yesterday and it was a bit slow, and need to be a bit optimized, but it worked.

I had some free minutes earlier and tried Jeremie’s shared code. I do not talk about what is saved and how, but it was… as fast as speedy gonzales, beep beep and Flash Gordon !

Muchas gracias amigo.

This was my first real use of xml in… seven years ?
(sometimes after REALbasic 5.5.5).

Text operations when using arrays are extremely fast in Xojo.

The most common error when handling a lot of text is the following:

Dim text As String
For i = 0 to 1000
text = text + EndofLine + someOtherText
Next i

Return Text

The code will run 100x (1000x ?) times faster this way:

Dim lines() As String
Redim lines(1000)
for i = 0 to 1000
lines(i) = someOtherText
Next i

Return Join(text, EndOfLine)

BTW: I get an error in rendering (to see what it looks) in both FireFox and Safari with a frient csv file (loaded in a ListBox and exported using the ExportToXML Function.

The offending thing is… an ampersand alone in the csv file (&). Note that the next line in the ListBox is the same entry excepted that the amersand is html coded (&).

I nearly forget: it was in an URL (http:// …/… and the ampersand appeard in the file name part of the URL).

Now, I have to read / watch carefully the shared code ;-:slight_smile: