Listbox w/ 3 columns: center 2, left align 1

Hi. I have a listbox with 3 columns. I want the first two columns to be centered and the last one to be left aligned. I found the following code snippet on the forum that is used in the CellTextPaint, and while this does center the contents, it centers all 3 columns. How can I center the first 2 and left align the 3rd?

if row>-1 and row<me.ListCount then 
  g.DrawString(me.cell(row,column),g.Width/2-g.StringWidth(me.cell(row,column))/2,y) 
  Return true 
end if

Did you know that you can set the alignment of a column?

ListBox.ColumnAlignmentAt

You could do something like this:

ListBox1.ColumnAlignmentAt(0) = ListBox.Alignments.Center
ListBox1.ColumnAlignmentAt(1) = ListBox.Alignments.Center
ListBox1.ColumnAlignmentAt(2) = ListBox.Alignments.Left

Your code doesn’t check the right column. You could also include And Column < 2 in your IF...THEN statement. Than your approach could work.

If you use this code in the CellTextPaint event of the listbox, you don’t need to check if the row is in the right range. The event is only raised when there is a valid cell.

You only need to check this if you use the CellBackgoundPaint event. Because that one will be raised on all cells, even on rows that don’t exist.

But I would go for the ColumnAlignmentAt way if I were you. Much easier to implement.

Boy do I feel foolish! I did see the https://documentation.xojo.com/api/deprecated/listbox.html#listbox-columnalignmentat page, but when trying to implement, I noticed Center does not autocomplete (2019 r3.2… maybe was corrected in 2020 r1). My only choices were Right or Left. I had never tried just typing it in and trying it. Thanks for your help Edwin!

1 Like

I don’t have Xojo open now. And I can’t check if the bug is fixed. Otherwise you could file a bug report in the Feedback app.

Or you could simply download the newer Xojo release :innocent:

I’ve been holding off to see if r2 was coming out soon since I didn’t really need anything for the r1 Web 2.0 updates, but r1 does have some listbox improvements and a few other things of interest that I might be downloading it soon anyway

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.