Forum > LCL

How to install a new property on a TGridColumn component?

<< < (2/3) > >>

dje:
Have you looked into TTIGrid? Its a RTTI enabled control derived from TCustomGrid. It seems to fit your requirement of "component objects can be associated with arbitrary data".

edit: It is located in the RTTI tab.

y.ivanov:

--- Quote from: Al3 on June 29, 2022, 01:47:00 am ---I figured it has to be something harder than usual.
It of course wouldn't worth the effort and I should probably find a different way to do that such as associating the column object with a set or something?

--- End quote ---
It depends on what you're aiming for. If you want to work with the added properties into the form designer it is a bit harder - you should create new component based on TCustomStringGrid or TCustomDrawGrid.


--- Quote from: Al3 on June 29, 2022, 01:47:00 am ---In CGTK component objects can be associated with arbitrary data which internally uses a hash table.
I wouldn't learn how to write components just when I started using OP again  :D
Also while TStringGrid displays string cells, I should be able to easily (re)format them based on the column data type once I find out how to associate it.

--- End quote ---

FYI, the TStringGrid have a Objects property along with the Cells:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    property Cells[ACol, ARow: Integer]: string read GetCells write SetCells;    property Objects[ACol, ARow: Integer]: TObject read GetObjects write SetObjects;and you can freely assign them to whatever descendant of TObject or whatever data fits in SizeOf(Pointer) you'd like. Just don't forget to Free them (if they are dynamic) when appropriate, on OnColRowDeleted and when exiting the form.


--- Quote from: Al3 on June 29, 2022, 01:47:00 am ---Another probably simpler, but less convenient way is to just grow an array dynamically depending on the number of columns and store data there.

--- End quote ---

TGridColumn has a Tag (most classes in LCL have it) and you can use it for encoding the data type:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TDataType = (dtString, dtInteger, dtFloat, dtDate);  // change as needed   ...  Grid.Columns[C].Tag := Ord(dtFloat);  ...  if TDataType(Grid.Columns[C].Tag) = dtFloat then ...;
And then you can hook on OnGetEditText/OnSetEditText to convert to/from text in Object[C,R]. Of course you must assign both Objects[C,R] and the text representation of Objects[C,R] in Cells[C,R] initially.

Al3:
So plenty of options. I wouldn't be able to know which one is optimal. What I do is focus on one and if it works - it works.
Long road to go to be able to review code in terms of performance, readability, room for bugs etc.

RTTI
I haven't checked it, because I wasn't familiar with the RTTI abbreviation. It did not sound like something that I could use.
A quick search reveals that people reported crashes when using it, I don't know what's the case particularly (maybe not self-explanatory to use?), but I also cannot find a documentation about it.
Edit: This looks useful to read: https://wiki.freepascal.org/RTTI_controls

At least I am sure that I won't subclass TStringGrid and I am starting to think this is not even needed.
I don't need that much of an extended functionality.
What I need is columns displaying cells in a certain format, depending on what the user wants them to be.
Hooking cell changes should let me reformat them, but there's one more potentially problematic scenario.
I am 100% sure I want to use OP for this project, but with GTK I can have checkboxes and images in cells.
So I am aiming to have these column types:
- bool (checkbox)
- integer
- real
- string
- color
- image
These will suffice, but I am more inclined to think that I will actually have to use TCustom or TDraw Grids for those. It sounds as if that's the case, but I haven't looked into them yet.

So I think the question remains - what control fits those requirements the most.
It'd be vrry cool to use one that is capable of doing all that for me

Al3:

--- Quote from: Handoko on June 29, 2022, 07:53:07 am ---There are many possible solutions, writing a child class inherited from TStringGrid, or modify all the column-related classes of TStringGrid, use a TDrawGrid as suggested by wp, even writing a new component from scratch.

Maybe you can draw some sketches showing what you want it to look like and explaining what features you need. So we can suggest you which solution suits your case. I may write it for you if that is not too complicated.

--- End quote ---

:)
I never actually offered something like that, but I could tell you what's my idea and if you like it, maybe we could write some code on it together in our spare time?
It's merely something that I want to do for sure, but have no specific time restriction, neither I am in a hurry. So maybe if you like the idea we can share credits on it and write some code that I can also learn from in the meantime. When the program is complete, I will use it a lot in a variety of different applications. Including in games and in gE2.

Handoko:
Writing components is a fun thing I like in programming. Tell me what already in your head and it will be more helpful if you can provide some pictures. You can post it here, or if you want you can PM me.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version