Formatting listbox

Hi,
I have a listbox,

Date              Check         Item Code    Item Name
20-10-2023        9001            C01        Green Car
20-10-2023        9002            C02        Brown Car
20-10-2023        9002            C03        Red Car

is it possible to change the format into,

Header           Check           Date
Item name        Item Code       Item Name
Header           9001            20-10-2023
Item name        C01             Green Car
Header           9002            20-10-2023
Item name        C02             Brown Car
Item name        C03             Red Car

I didn’t find anything unique to code.

any help?

thanks
arief

Check out the sample code in the Documentation for “WebListBoxStyleRenderer”. You can populate a listbox and call (apply) a Style to a cell (or whole row).

William, I’m not sure Arief is talking about WebListbox.

I’m sorry Arief, I don’t understand what you need.

OMG I’ve got Web on the brain today! Thanks!

Take a look here…

https://www.dropbox.com/s/e1ex2crgkk7btbe/Rod1.xojo_binary_project?dl=1

Hi @Arief_Sarjono,

Not quite clear on what you’re asking. What specifically are you having trouble with?

at desktop apps you have cell paint events you can draw whatever you like by data(object) in cell tag or row tag.

Hi,

I was trying to change the format of listbox1 content, into new format which is contain Header and Item.

here is the code in button pressed,

var rowMax as Integer = ListBox1.LastRowIndex
var rowIndex, checkNumber, lastCheckNumber as Integer
while rowIndex <= rowMax
  checkNumber = ListBox1.CellTextAt(rowIndex, 1).ToInteger
  if checkNumber > lastCheckNumber then
    if lastCheckNumber > 0 then
      ListBox1.AddRowAt(rowIndex, "Header")
      rowIndex = rowIndex + 1
      rowMax = rowMax + 1
    end if
    ListBox1.AddRowAt(rowIndex, "Item")
    rowIndex = rowIndex + 1
    rowMax = rowMax + 1
    
    dim gg as integer
    gg=rowMax+2
    
    lastCheckNumber = checkNumber
  end if
  rowIndex = rowIndex + 1
wend

and the listbox celltextpaint code

'Listbox1.InvalidateCell(row,column)
if Listbox1.CellTextAt(row,0)="Header" then
  dim gg as integer
  gg=row+2
  Listbox1.CellTextAt(row,1)=Listbox1.CellTextAt(gg,1)
  Listbox1.CellTextAt(row,2)=Listbox1.CellTextAt(gg,2)
  Listbox1.CellTextAt(row,3)=Listbox1.CellTextAt(gg,3)
  
  
end if


I dont know how to move it into the right position.

thanks
arief

paint events using the graphics contect to draw the content

Hi
I have updated the code, but still cant move the value from Listbox1(row,1) into Listbox1(row,3) -1

Is there any other easy way than using celltextpaint?

thanks
arief

https://transfer.sh/G8zldtzpLF/listboxformatting.xojo_binary_project

how about this?

Public Sub Transform(lb As DesktopListBox)
  If lb.HeaderAt(0)<>"Date" Then
    MessageBox("list is already transformed")
    Return
  End If
  
  Var headers() As ClassHeader
  
  Var lastCheck As String = ""
  Var header As ClassHeader
  For row As Integer = 0 To lb.LastRowIndex
    Var cellDate As String = lb.CellTextAt(row,0)
    Var cellCheck As String = lb.CellTextAt(row,1)
    If cellCheck <> lastCheck Then
      header = New ClassHeader
      header.Check = cellCheck
      header.Date = cellDate
      headers.Add(header)
      lastCheck = cellCheck
    End If
    Var item As New ClassItem
    item.Code=lb.CellTextAt(row,2)
    item.Name=lb.CellTextAt(row,3)
    header.Items.Add(item)
  Next
  
  lb.RemoveAllRows
  lb.ColumnCount = 2
  lb.HeaderAt(0)="Check / Item Code"
  lb.HeaderAt(1)="Date / Name"
  Var p As Pair
  For Each header In headers
    p = New Pair(header.Check,header.Date)
    lb.AddRow(p.Left, p.Right)
    For Each item As ClassItem In header.Items
      p = New Pair(item.Code,item.Name)
      lb.AddRow(p.Left, p.Right)
    Next
  Next
  
  
  
End Sub

ClassHeader

Public Property Date As String
Public Property Check As String
Public Property Items() As ClassItem

ClassItem

Public Property Code As String
Public Property Name As String

Ashampoo_Snap_Freitag, 20. Oktober 2023_20h47m57s_001_Untitled

Ashampoo_Snap_Freitag, 20. Oktober 2023_20h48m13s_001_Untitled

it looks maybe better if the result use 4 columns without mixed content.

Hi,
yes this is more easier. thanks!
but actually i need it to do in more columns. not just two columns.

will it be possible to be transformed into more columns

Var p As Pair

What is pair mean, never heard about this.

will do some modification with this code.

thanks
arief

Never used, but the documentation entry is here:
https://documentation.xojo.com/api/data_types/pair.html#pair

1 Like

you not need, just do

    lb.AddRow(header.Check,header.Date,)

[quote=
Another alternative you can consider is piDog’s DataView class. It is pure Xojo code built upon a canvas, not subclassed from a ListBox. So it was able to implement TONS of stuff not easily supported by the standard Listbox class.
[/quote]

FWIW … Once I got DataView some years back now, I NEVER used a single Xojo listbox again. The functionality was clearly superior and I never encountered a problem that could not be solved with the component. Jim McKay (creator and proprietor of DataView) is easily accessible and helped me through a number of tough spots (sometimes even making a change to his software, creating a new revision in the process).

3 Likes

Hi,

I have tried to add 2 columns more, but it still tells that only 2 arguments allowed.

I add new properties, still got the same error message.

how to modified this line code,

lb.HeaderAt(0)="Check / Item Code"
lb.HeaderAt(1)="Date / Name"
Var p As Pair
For Each header In headers
  p = New Pair(header.Check,header.Date,header.test)
  lb.AddRow(p.Left, p.Right)
  For Each item As ClassItem In header.Items
    p = New Pair(item.Code,item.Name,item.testclass)
    lb.AddRow(p.Left, p.Right)
  Next
Next

thanks
arief

Pairs have two variables: Left, and Right.

you construct a pair with two arguments:

Var p As Pair = new Pair(LeftStuff, RightStuff)

You’re trying to define a pair with three arguments:

p = New Pair(header.Check, header.Date, header.test)

remove the pair

just do

  lb.AddRow(header.Check,header.Date,header.test)

Hi,

I have remove the pairs,
but the result is not as expected.

customer data was in cell 4.

If lb.HeaderAt(0)<>"Date" Then
  MessageBox("list is already transformed")
  Return
End If

Var headers() As ClassHeader

Var lastCheck As String = ""
Var header As ClassHeader
For row As Integer = 0 To lb.LastRowIndex
  Var cellDate As String = lb.CellTextAt(row,0)
  Var cellCheck As String = lb.CellTextAt(row,1)
  If cellCheck <> lastCheck Then
    header = New ClassHeader
    header.Check = cellCheck
    header.Date = cellDate
    headers.Add(header)
    lastCheck = cellCheck
  End If
  Var item As New ClassItem
  item.Code=lb.CellTextAt(row,2)
  item.Name=lb.CellTextAt(row,3)
  item.Customer=lb.CellTextAt(row,4)
  header.Items.Add(item)
Next

lb.RemoveAllRows
'lb.ColumnCount = 3
lb.HeaderAt(0)="Check / Item Code"
lb.HeaderAt(1)="Date / Name"
'Var p As Pair
For Each header In headers
  lb.AddRow(header.Check,header.Date)
  For Each item As ClassItem In header.Items
    lb.AddRow(header.Check,header.Date,header.Pelanggan)
  Next
Next

where are the mistakes?

thanks
arief

I do not understand; the syntax, according to the documentation is:
AddRow(ParamArray Item As String)

Given example is: ListBox1.AddRow("October")
and to fill four columns:
Listbox1.AddRow("Sept", "Oct", "Nov", "Dec")