Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi : Setting OnGetText Event Handler for fields of a dynamic query

I want to set my own procedure to OnGetText event of fields in a dynamic query

My procedure is like this :

procedure TMainFrm.MyFieldGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin

  ...

end;
  • "...Captions" are String array constants

I set the event handler in OnAfterOpen event of ADOQuery :

procedure TImportFrm.ADOQueryAfterOpen(DataSet: TDataSet);
var
 I : Integer;
begin
 for I := 0 to ADOQuery.FieldCount - 1 do
  ADOQuery.Fields[I].OnGetText := MainFrm.MyFieldGetText;
end;

But after opening ADOQuery , there is no Text to display , it looks like the Text value is empty !

It seems it doesn't matter what my procedure do , because when I set an empty procedure ( with no code ) , no text displayed too

what goes wrong ?

thanks ...

like image 236
Mahmoud_Mehri Avatar asked Jul 29 '15 04:07

Mahmoud_Mehri


2 Answers

Try this:

procedure TMainFrm.MyFieldGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
  if Sender.FieldName = 'XX' then
   begin
     Text := .... String(Sender.Value);// ( or Text := Sender.AsString);
   end;
  if Sender.FieldName = 'YY' then
   begin
     Text := .... String(Sender.Value);// ( or Text := Sender.AsString);
   end;
  ...

end;
like image 77
Val Marinov Avatar answered Nov 05 '22 00:11

Val Marinov


Thanks all

The problem was that I should mention all situations of Text and should use Sender.value instead of Text in right side ! , I changed my procedure to this and problem solved :

procedure TMainFrm.MyFieldGetText(Sender: TField; var Text: String;
  DisplayText: Boolean);
begin
 if Sender.AsVariant = Null then
  Exit;

 Text := Sender.AsString;

 if MatchStr(Sender.FieldName, BooleanFieldNames) then
  Text := BooleanCaptions[Sender.AsInteger];

 if Sender.FieldName = 'BNStatus' then
  Text := BNStatusCaptions[Sender.AsInteger];

 if MatchStr(Sender.FieldName, ['FStatus', 'LStatus', 'FirstStatus']) then
  Text := FLStatusCaptions[Sender.AsInteger];

 if Sender.FieldName = 'PayType' then
  Text := PayTypeCaptions[Sender.AsInteger];

 if Sender.FieldName = 'BGType' then
  Text := BGTypeCaptions[Sender.AsInteger];

 if Sender.FieldName = 'Updated' then
  Text := UpdatedCaptions[Sender.AsInteger];

 if Sender.FieldName = 'DieUser' then
  Text := DieUserCaptions[Sender.AsInteger];

 if Sender.FieldName = 'LiveUser' then
  Text := LiveUserCaptions[Sender.AsInteger];

 if Sender.FieldName = 'NVStatus' then
  Text := NVStatusCaptions[Sender.AsInteger];

 if Sender.FieldName = 'BSGender' then
  Text := BSGenderCaptions[Sender.AsInteger];

 if Sender.FieldName = 'BSMType' then
  Text := BSMTypeCaptions[Sender.AsInteger];

end;

Thanks again ...

like image 37
Mahmoud_Mehri Avatar answered Nov 05 '22 00:11

Mahmoud_Mehri