我正在编写一个Delphi专家。我需要能够将值写入对象属性上的属性。例如,我在表单上有一个GroupBox并且我想编辑Margins.Left属性。我使用以下过程来完成它,但是它会在标记的行上出现AV错误。
该过程从(属性编辑器中)获取组件、属性名称(例如“Margins.Left”)和新值,解析出属性名称,获取对象,读取当前值并尝试更改它(如果不同)。然后它调用一个方法来记录任何更改。
该过程从(属性编辑器中)获取组件、属性名称(例如“Margins.Left”)和新值,解析出属性名称,获取对象,读取当前值并尝试更改它(如果不同)。然后它调用一个方法来记录任何更改。
procedure EditIntegerSubProperty(Component: IOTAComponent;const PropName: String;NewValue: Integer);
var AnObject: TObject;
TK: TTypeKind;
At: Integer;
AClassName, APropName: String;
PropInfo: PPropInfo;
OldValue: Integer;
begin
At := Pos('.', PropName);
if At < 1 then
raise Exception.Create('Invalid SubProperty Name: '+PropName);
AClassName := Copy(PropName, 1, At-1);
APropName := Copy(PropName, At+1, length(PropName));
TK := Component.GetPropTypeByName(AClassName);
if TK <> tkClass then
EXIT;
AnObject := GetObjectProp((Component as INTAComponent).GetComponent, AClassName);
if PropIsType(AnObject, APropName, tkInteger) then
begin
OldValue := GetInt64Prop(AnObject, APropName);
if OldValue <> NewValue then
begin
SetInt64Prop(AnObject, APropName, NewValue); <----AV HERE
ChangeLogInteger(Name, PropName, OldValue, NewValue);
end;
end;
end;