Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Colored Rows in Listbox: Now with Multiple Boxes per Listbox Row  (Read 686 times)

Boleeman

• Hero Member
• Posts: 594
Colored Rows in Listbox: Now with Multiple Boxes per Listbox Row
« on: July 11, 2024, 04:35:47 pm »
Please go down to last replies for multi-boxed version. Yay. I achieved it.

I was investigating how to make, load and save colored rows to a normal TListbox.

Got the idea from https://forum.lazarus.freepascal.org/index.php?topic=48660.0

Can also change the colored row order.

How can we make the colored part only a partial rectangle of that row, and not the full row size?

and have the text appear after the colored rectangular bit.
« Last Edit: July 12, 2024, 11:23:52 am by Boleeman »

wp

• Hero Member
• Posts: 12192
Re: Colored Rows in Listbox: Make a smaller rect coloured in row
« Reply #1 on: July 11, 2024, 05:31:28 pm »
In the OnDrawItem event, first fill the entire rectangle given as parameter with the background color (your first two lines)

Define a temporary rectangle (R) equal to ARect. Set R.Right := ARect.Left + w, where w is the requested width of the color box. Maybe, to get a square, select w := ARect.Height. Shrink the size of R by a few pixels to avoid the color boxes of the lines touching themselves (InflateRect(R, -2, -2). Fill that smaller R with the requested color.

In order center the text vertically, calculated the text height: h := Listbox.Canvas.TextHeight('Tg'). Then calculate the vertical text position: y := (ARect.Top + ARect.Bottom - h) div 2. Finally draw the text: Listbox.Canvas.TextOut(R.Right + (some distance), y, Listbox.Items[Index])

Boleeman

• Hero Member
• Posts: 594
Re: Colored Rows in Listbox: Make a smaller rect coloured in row
« Reply #2 on: July 11, 2024, 08:11:56 pm »
Kinda got there.

Boleeman

• Hero Member
• Posts: 594
Re: Colored Rows in Listbox: Make a smaller rect coloured in row
« Reply #3 on: July 12, 2024, 06:42:16 am »
Some progress made. A bit different to WP method.

Would be nice to make a Tcomponent out of it. Not really sure exactly how though.

Anyhow, still playing around and experimenting.

An edit listbox row.
(by double clicking a listbox row, changing the TColorbutton color and TEdit text and clicking enter button)

To Add a new value, click into the TEdit and press Escape. This will unfocus the focus rectangle on the TListbox

« Last Edit: July 13, 2024, 12:35:50 am by Boleeman »

Boleeman

• Hero Member
• Posts: 594
Re: Colored Rows in Listbox: Make a smaller rect coloured in row
« Reply #4 on: July 12, 2024, 10:16:23 am »
Ah, got the multi-boxed version happening. Hooooreeeey

Creates listbox rows of squares.

Saves listbox contents.

« Last Edit: July 12, 2024, 10:24:44 am by Boleeman »

Boleeman

• Hero Member
• Posts: 594
Re: Colored Rows in Listbox: Now with Multiple Boxes per Listbox Row
« Reply #5 on: July 12, 2024, 11:26:20 am »
And finally I have the multiple boxed version working.

The main bits seem to be working.

Set number of boxes.
Select colors of boxes and insert them and the TEdit text into a listbox row.
Move rows up/down.
Edit rows by double clicking, changing and then press Enter button.
Add new items by clicking in TEdit and press escape. Then add new items.
Save listbox items to txt.

I assume if I create 5 boxed rows then the save and open txt will work for that amount that was set.

I had the Lazarus IDE debugger seize for some reason, so perhaps some memory somewhere is not being freed. Not sure?
I am using 32-bit Windows Lazarus Compiler on this code.

Hopefully someone can add some more features onto the program or perhaps make it work more efficiently.

See you all for now. Cheers.
« Last Edit: July 12, 2024, 11:49:15 am by Boleeman »