csharpdefector
csharpdefector

Reputation: 833

TDrawGrid OnSetEditText fires twice + Inplace Editors + TCustomGrid

I have (in D2010) a TDrawGrid on my form. Im handling my OnDrawCell, OnSetEditText, OnSetEditText etc alls gfine there.

If however in the specific situation that you go in a cell that has some text, highlight the text in its entirity, then type some character to replace. Now the OnSetEditText event fires twice in a row from one keypress, firstly with a blank string, then again with a string containing the character you type. Is this correct or a bug? I would have expected to just get the event fired once with the string containing my single character typed.

I am using OnSetEdit text to set other class properties which does stuff like validation, so when the above situation causes my other code to consider momentarily my class properties to be invalid, before imediately being set back to valid again on the 2nd fire, its still having undesriable consequences though and i want to stop that 1st event firing, or get the bets workaround I can.

Now whilst on the subject of grids, may I appeal to you folks for an helpful tips in the folling things. I am fairly new to deplhi from c# (I'm going the other way!) but Im finding the docs to be pretty thin on the ground, and am getting surprisingly limited results googling for things, so your help is really appreciated.

1) Custom Inplace Editors for TDrawGrid - any tips or good links appreciated! 2) For custome inplace editors, am i better off with TDrawGrid or descending my own control from TCustomGrid and going from there? 3) TCustomGrid. I am getting nowhere here... If I create a new component and descend from TCustomGrid I just get an 'abstract error' when i place it my form. Therefore further experimentation is very much canclled - an advice on just even getting started with TCustomGrid appreciated!

My plan is first to get combo boxes (in virtual mode) working as cell editors. Thats a standard VCL control. Afterwards I plan to create my own control based around a virtual combobox but with a search thing at the top to filter the list down (a bit like in the Delphi IDE Tool pallette), and use this component as inlace editor if possible. Im a fair way off that right now! Thanks all

Edit: Remy - Here are my two call stacks from beak point in OnSetEditText. Left is first fire with the empty string, right is 2nd fire with correct string value. The 5 truncated lines in the middle are all references to comctl32.dll in both. Ty.

Click here for call stack

Upvotes: 2

Views: 1461

Answers (2)

mg30rg
mg30rg

Reputation: 1349

On "abstract error"s:

In Delphi the cause of an "abstract error" is the fact you try to instantiate a class with a non-overridden virtual abstract method. If you see such errors, you should look at the definitions of the parent object (TCustomGrid in this case), to see which of its methods are virtual abstract, then you should override those function in your descendant class.

Note, that Delphi (poorly) only requires you to override functions that gets called, therefore lots of coders out there don't even know the cause of the mentioned behavior.

Upvotes: -1

Remy Lebeau
Remy Lebeau

Reputation: 595971

The OnSetEditText event is fired whenever the inplace editor's contents are updated for any reason, when a drop-down editor is closed after selecting a different value, a drop-down editor is double-clicked on, or a drop-down editor's RestoreContents() method is called. So you very likely are getting multiple actions updating the editor one after the other. I suggest you put a breakpoint inside the TCustomDrawGrid.SetEditText() method and see what the call stack looks like each time the event is being fired.

Regarding #2, it does not matter what you derive from. Any TCustomGrid descendant can have a custom inplace editor. Simply override the virtual CreateEditor() method.

Regarding #3, if you are getting an abstrct error when deriving from TCustomGrid directly, then you did not override its abstract methods correctly.

A grid already natively supports a drop-down inplace editor that mimics a combobox. Look at the TInplaceEditList class. You can use the OnGetPickListItems event to fill in the editor with values. Also, have a look at how TValueListEditor implements its custom editors.

Upvotes: 2

Related Questions