* * *

Author Topic: TDrawGrid problems  (Read 1077 times)


  • Full Member
  • ***
  • Posts: 211
TDrawGrid problems
« on: April 19, 2017, 01:39:00 pm »
I have a TDrawGrid, with OnDrawCell working fine. Now I'm trying to get cell editing working. Actually I have another application in which this is working fine, but I cannot figure out why my current application, using (so I think) the same methd, does not work. It would be helpful if someone could set out the minimum required event handling. I've looked at the availavle documentation, and compared at length my two applications, but I'm now stuck.

My grid contains just strings, but for various reasons I want to use TDrawGrid rather than TStringGrid, which I have successfully used in another Lazarus app and fairly extensively in Delphi apps.

One question, do I need to use OnSelectEditor, and if so, what do I set Editor to to get a simple string editor?



  • Hero Member
  • *****
  • Posts: 4602
Re: TDrawGrid problems
« Reply #1 on: April 19, 2017, 04:06:47 pm »
In the attachement there's a little demo showing how to edit cells in a DrawGrid. First of all, you need some storage from which you get the strings to be displayed and to where you have to return the edited strings. In the demo, it is a simple 2D array of string, but of course, you can conceive anything more suitable to your needs.

When editing begins the grid calls the event handler OnGetEditText to determine the string which is passed on the the cell editor. My event handler just reads the string from the corresponding element of the 2d string array.

When text is typed in, the event OnSetEditText is generated. It has the current text in the GridEditor as a parameter. You could use this event to write the string back to the 2d string array. There is a problem, though, if the user presses ESC afterwards. To me this always means that the user wants to cancel editing and return to the original string. But we had lost the original string during typing... To avoid this I use the onSetEditText differently: it copies the current edit text to a temporary variable. I use the OnEditingDone event to write the temporary variable to the string storage, but only if ESC has not been pressed. To detect this there's an OnKeyPress handler which sets the variable FCancelled to true in this case.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10


  • Full Member
  • ***
  • Posts: 211
Re: TDrawGrid problems
« Reply #2 on: April 19, 2017, 04:53:38 pm »
I've found the problem.

In the app that worked I had  {$mode objfpc}{$H+} in the units that contained grid-related routines.
In the app that did not work I had H- in one of the units and nothing in the other.#

So my conclusion is that as long as I have H+ in all relevant units, then it works. (Not sure whether H-
 in both would work). I assume this is because some event handlers, such as OnGetEditText, have parameters of type "string" which the H option causes to be interpreted in different ways.



Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus