Examples Delphi

Title: Lightweight Object Browser
Question: How to display object properties in a string grid
Answer:
procedure ObjectToStringGrid(obj:TObject; sg:TStringGrid);
var
prop_count: Integer; // Anzahl der published properties
PropInfos: PPropList;
i: Integer;
Value : Variant;
PropTypeName : string;
method : TMethod;
begin
Assert(Assigned(sg));
Assert(Assigned(obj));
sg.ColCount := 3;
sg.FixedCols := 1;
sg.FixedRows := 1;
sg.Cells[0, 0] := 'Property';
sg.Cells[1, 0] := 'Value';
sg.Cells[2, 0] := 'Prop Type';

// tkAny
// tkProperties
prop_count := GetPropList(obj.ClassInfo, tkAny, nil);
sg.RowCount := prop_count + 1;
GetMem(PropInfos, prop_count * SizeOf(PPropInfo));
try
GetPropList(obj.ClassInfo, tkProperties, PropInfos);
for i := 0 to prop_count - 1 do
begin
sg.Cells[0, i+1] := PropInfos^[i].Name;
PropTypeName := PropInfos^[i].PropType^.Name;
Value := GetPropValue(obj, PropInfos^[i].Name, True);
if PropInfos^[i].PropType^.Kind = tkMethod then
begin
method := GetMethodProp(obj, PropInfos^[i].Name);
if Assigned(method.Code) then
sg.Cells[1, i+1] := TObject(method.Data).ClassName+
'.'+ TObject(method.Data).MethodName(method.Code)
else
sg.Cells[1, i+1] := 'nil';
end
else if PropTypeName = 'TDateTime' then
sg.Cells[1, i+1] := DateTimeToStr(Value)
else
sg.Cells[1, i+1] := string(Value);
sg.Cells[2, i+1] := PropTypeName;
if not IsStoredProp(obj, PropInfos^[i]) then
sg.Cells[2, i+1] := sg.Cells[2, i+1]+': not stored';
end;
finally
FreeMem(PropInfos);
end;
end;
Please report your suggestions to a_j_schmidt@rocketmail.com