TOF : Listbox Numbering ; UPDATED

Wouldn’t this be simpler???

Function CellTextPaint(g As Graphics, row As Integer, column As Integer, x as Integer, y as Integer) Handles CellTextPaint as Boolean
  If row>=Me.ListCount Or column<>0 Then Return False
  If row=0 Then 
    Me.cell(row,0)="1"
  Else
    x=Val(Me.cell(row-1,0))
    If Me.cell(row,1)<>Me.cell(row-1,1) Then x=x+1
    Me.cell(row,0)=Str(x)
  End If
  Return False
  
End Function

no loop… update is live, and looks at only two rows

What if there is an apple added in row 9? I feel both “solutions” are a poor design and the OP should have been asked what his goal actually is.

A good design might be a custom class fruit with a name as string and number as integer, a lookup dictionary with the name as key and the number as value, and a constructor that automatically sets the number

This event driven code adapts to the contents of the listbox as rows are added or deleted or sorted… It DOES ASSUME (as shown on TOF) the like values are grouped together

Which is the point I’m making - it is the same minimalist approach to advise by MVPs and others that Xojo has to everything else (aka “not good but good enough”). Why not teach the guy better programming when the opportunity arises? Give him what he NEEDS instead of only what he thinks he WANTS?

I think THAT might be the basic problem I (and possibly others here) have with Xojo - we want it to be good, maybe even great - Xojo is happy with “good enough” …

I see no point to be made… Since the OP did not provide detailed technical specification, and his post “infered” grouped data to start with, I went with the data I had available…

So what “better” is there to “teach”… he stated a problem, provided information in the way of an example output,

Not sure what your rant has to do with MVP and or Xojo itself.

The other “solutions” posted on TOF did the same thing as my solution, however they have to iterate the entire listbox, and have to be manually controlled.

Please feel free to post the CORRECT SOLUTION

“You can lead a horse to water, but you cannot make it drink” … :wink:

I tend to agree with Dave that its hard to know what “better” solution might be suggested to the OP given the limited information they gave in their original post

Markus’s suggestion might be appropriate
I honestly don’t know based on the limited info

Beginners always use the Listbox as a data storage facility. It was about a decade before I started to use it in a more object-oriented fashion.

I think learning a more object-oriented approach early on - especially with a simple example - would be a huge benefit … :thinking: … learning always tends to be … :grin:

Are you interested in learning why Object-Oriented Programming is Bad ? :face_with_raised_eyebrow:

No :stuck_out_tongue:

But, if the user scrolls the listbox and then makes changes, the CellTextPaint event won’t be called for the first rows. Your code then would discard the items not displayed.