Hi taazz,
In my test code that I set up to figure this out, I used the TForm1.FormShow event handler
procedure TForm1.FormShow(Sender: TObject);
begin
AdsTable1.FieldByName('EntryType').OnGetText:=@TForm1.AdsTable1OnGetText;
end;
Is this incorrect?
I am not sure how FPC handles this kind of things but in my mind Tform1 is a class not an object I do not know if an object has been created for that class unless I use a variable that has a reference to an object of TForm1 class. This means that it needs extra checking in order to make sure that everything is in place or you will get various SIGSEGV errors.
Are you suggesting:
procedure TForm1.FormShow(Sender: TObject);
begin
AdsTable1.FieldByName('EntryType').OnGetText:=@Self.AdsTable1OnGetText;
end;
although self in this context is not needed it makes more sense to me and does not raise any red flags when I read it.
So yes its a lot better from my point of view
One more question:
Do I need to also create an OnSetText event handler? Such as
procedure TForm1.AdsTable1OnSetText(Sender: TField; var aText: string; DisplayText: boolean);
begin
case Sender.AsString of
'New': aText := '0';
'Reverse': aText := '1'
end
end;
The user cannot edit directly in the DBGrid, so I don;t know if its needed. I believe I have the DBGrid set to nto allow editting.
Thanks
You do create a OnSetText event if you want to allow the user to enter something other than the expected value for example
if you want the end user to be able to type in New, N or Reverse, R and translate it to 0 or 1 then you use the OnSetText event.
If you use a DBcomboBox for the selection then you ignore the onsetText and work with the combobox it self. In any case when the OnSetText event is fired Sender.asstring will contain the existing value not the new one so you can't use it to check for the new value in this case you need to use the aText variable.
eg
procedure TForm1.AdsTable1OnSetText(Sender: TField; var aText: string; DisplayText: boolean);
begin
case UpperCase(aText) of
'NEW', 'N' : aText := '0';
'REVERSE', 'R' : aText := '1';
end;
end;
In my defense I have only used those 2 events on password fields where I encrypt and decrypt them on the fly on the user edit form and the ongettext only to show some data inside a grid for a memo fields.
Regards.