Показать сообщение отдельно
  #3  
Старый 14.03.2013, 12:25
Аватар для morebeauty
morebeauty morebeauty вне форума
Начинающий
 
Регистрация: 21.06.2012
Сообщения: 106
Версия Delphi: Delphi 7
Репутация: 10
По умолчанию

Если кому интересно, вот такой получился класс. Правда не все методы еще реализовал, но основа готова.

Код:
type
  TComp = record
    Component:TComponent;
    P:String;
    Enabled:Boolean;
    Link:Boolean;
    Variable:string;
  end;

  TComps = array of TComp;

  TLinking = class
  private
    FSL:TStringList;
    FComponents:TComps;
    function GetVal(name: string): string;
    procedure SetVal(name: string; const Value: string);
    function GetCCount: Integer;
    function GetProperty(AControl: TPersistent; AProperty: string): PPropInfo;
  public
    property Value[name:string]:string read GetVal write SetVal; default;
    property CCount:Integer read GetCCount;
    function AddComponent(const C:TComponent;P,V:String):Boolean;
    constructor Create;
    destructor Destroy;
  end;

implementation

function TLinking.AddComponent(const C: TComponent; P, V: String): Boolean;
begin
  SetLength(FComponents,Length(FComponents)+1);
  FComponents[High(FComponents)].Component:=c;
  FComponents[High(FComponents)].P:=p;
  FComponents[High(FComponents)].Enabled:=True;
  FComponents[High(FComponents)].Link:=False;
  FComponents[High(FComponents)].Variable:=v;
end;

constructor TLinking.Create;
begin
  inherited;
  FSL:=TStringList.Create;
end;

destructor TLinking.Destroy;
begin
  FSL.Free;
  inherited;
end;

function TLinking.GetCCount: Integer;
begin
  result:=Length(FComponents);
end;

function TLinking.GetProperty(AControl: TPersistent;
  AProperty: string): PPropInfo;
var
  i: Integer;
  props: PPropList;
  typeData: PTypeData;
begin
  Result := nil;
  if (AControl = nil) or (AControl.ClassInfo = nil) then
    Exit;
  typeData := GetTypeData(AControl.ClassInfo);
  if (typeData = nil) or (typeData^.PropCount = 0) then
    Exit;
  GetMem(props, typeData^.PropCount * SizeOf(Pointer));
  try
    GetPropInfos(AControl.ClassInfo, props);
    for i := 0 to typeData^.PropCount - 1 do
    begin
      with Props^[i]^ do
        if (Name = AProperty) then
          result := Props^[i];
    end;
  finally
    FreeMem(props);
  end;
end;

function TLinking.GetVal(name: string): string;
begin
  Result:=FSL.Values[name];
end;

procedure TLinking.SetVal(name: string; const Value: string);
var
  i:Integer;
  PropInfo:PPropInfo;
begin
  FSL.Values[name]:=Value;
  for i:=0 to CCount-1 do
  begin
    if LowerCase(FComponents[i].Variable)=LowerCase(name) then
    begin
      PropInfo := GetProperty(FComponents[i].Component, FComponents[i].P);
      if PropInfo <> nil then
        SetStrProp(FComponents[i].Component, PropInfo, Value);
    end;
  end;
end;

Ну и работает как то так:

Код:
procedure TForm1.FormShow(Sender: TObject);
begin
  Linking:=TLinking.Create;
//Далее Edit1.Text и Label1.Caption связываются с некоторой переменной Var1
  Linking.AddComponent(Edit1,'Text','Var1'); 
  Linking.AddComponent(Label1,'Caption','Var1');
end;

procedure TForm1.btn1Click(Sender: TObject);
begin
  Linking['Var1']:='123';
//Меняется переменная Var1 - меняются привязанные к ней свойства компонентов
end;

На этом сайте так же нашел способы изменения свойств разного типа (не только string)
http://www.delphisources.ru/pages/fa...ps_values.html
Ответить с цитированием