I have written my own SQLite Database Manager and have very similar code to Karen’s code. I have made my dbListbox recognise the type of data being shown so as to display it as a user would read it.
A very useful property within the Recordset variable is ColumnType. By testing the ColumnType of each column in the dataset you can format the data. For example, if the RS.ColumnType(0) = 11 then the column is a Xojo Currency value. A currency value would (normally) be displayed '#,##0.00;(#,##0.00);\ '. So, Karen’s display variable of ‘theStr’ would be set to Format(Rs.IdxField(i).CurrencyValue, "#,##0.00;(#,##0.00);\ ") AND the column would be aligned right with ColumnAlignment = Listbox.AlignRight.
So I would adjust Karen’s code as follows:
[code]#tag Class
Protected Class RS_Listbox
Inherits Listbox
#tag Method, Flags = &h0
Sub ShowRecordSet(RS as RecordSet, Maxwidth as integer = 500)
DeleteAllRows
If RS is NIL Then
ColumnCount = 1
Column(0).WidthExpression = "*"
Heading(0) = "NIL Record Set"
AddRow "None Found"
Return
End IF
Dim ub as Integer = RS.FieldCount
ColumnCount = ub
Dim ColWidth() as double, theStr as String
Redim ColWidth(ub-1)
Dim P as new Picture(1,1,32)
Dim g as Graphics = P.Graphics
g.TextFont= TextFont
g.TextSize = TextSize
g.Bold = Bold
g.Italic = Italic
g.Underline = Underline
g.TextUnit = TextUnit
Dim i as Integer
For i = 1 to ub
theStr = RS.IdxField(i).Name
ColWidth(i-1) = g.StringWidth(theStr)
Heading(i-1) = theStr
// ADDED CODE FOR FORMATTING
select case RS.IdxField(i).ColumnType
case 0, 1, 4, 5, 14, 15, 16, 18, 255
ColumnTag(i-1) = "String"
case 6, 7, 13
ColumnTag(i-1) = "Number"
ColumnAlignment(i-1) = Listbox.AlignRight
case 2, 3, 19
ColumnTag(i-1) = "Integer"
ColumnAlignment(i-1) = Listbox.AlignRight
case 8, 9, 10
ColumnTag(i-1) = "Date"
ColumnAlignment(i-1) = Listbox.AlignCenter
case 11
ColumnTag(i-1) = "Currency"
ColumnAlignment(i-1) = Listbox.AlignRight
case 12
ColumnTag(i-1) = "Boolean"
ColumnAlignment(i-1) = Listbox.AlignCenter
end select
// END OF ADDED CODE
Next
Dim Row as Integer
While Not RS.EOF
// theStr = RS.IdxField(1).StringValue <-- REMOVE THIS
AddRow
Row = LastIndex
for i = 1 to ub
// theStr = Rs.IdxField(i).StringValue <-- REMOVE THIS
// ADDED CODE
select case ColumnTag(i-1)
case "String"
theStr = RS.IdxField(i).StringValue
case "Number"
theStr = Format(RS.IdxField(i).DoubleValue, "-#,##0.########")
case "Integer"
theStr = Format(RS.IdxField(i).IntegerValue, "-#")
case "Date"
theStr = RS.IdxField(i).DateValue.SQLDateTime
case "Currency"
theStr = Format(Rs.IdxField(i).CurrencyValue, "#,##0.00;(#,##0.00);\\ ")
case "Boolean"
if RS.IdxField(i)..BooleanValue then
theStr = "True"
else
theStr = "False"
end if
end select
// END ADDED CODE
Cell(row,i-1) = theStr
ColWidth(i-1) = Max(ColWidth(i-1), g.StringWidth(theStr))
Next
RS.MoveNext
Wend
ColWidth(0) = Max(ColWidth(0), g.StringWidth(theStr))
for i = 1 to ub
theStr = Str(min(Ceil(ColWidth(i-1)) + 10, Maxwidth))
Column(i-1).WidthExpression= theStr
Column(i-1).WidthActual= Val(theStr)
Next
Self.Invalidate
End Sub
[/code]
I have added some additional properties for the formatting that I have in the inspector with the defaults as shown above. This allows the developer to specify the actual format to use for each type of format. The formatting properties I created are:
formatInteger = "-#"
formatNumber = "-#,##0.########"
formatCurrency = "#,##0.00;(#,##0.00);\\ "
I have also created a date formatting routine so I have a formatDate property too.
I hope that you will find that you now have a completely data aware Listbox subclass.