Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   OOP (https://www.clubdelphi.com/foros/forumdisplay.php?f=5)
-   -   Como pasar un dato entre dos clases del tipo TProperty (https://www.clubdelphi.com/foros/showthread.php?t=51838)

egostar 31-12-2007 20:21:19

Como pasar un dato entre dos clases del tipo TProperty
 
Hola amigos

Sigo con el tema de creación de componentes y tengo el siguiente problema.

Tengo dos clases, una del tipo TStringProperty y la otra del tipo TPropertyEditor.

Código Delphi [-]
 
Type
  TKeyProperty = class (TStringProperty)
    function GetAttributes : TPropertyAttributes; override;
    procedure GetValues(Proc : TGetStrProc); override;
    function GetValue : string; override;
    procedure SetValue(const Value: string); override;
  end;
 
  TSubKeyProperty = class(TPropertyEditor)
    function GetAttributes : TPropertyAttributes; override;
    function GetValue : string; override;
    procedure Edit; override;
  end;
  1. La clase TKeyProperty la asigne como [paValueList]
  2. La clase TSubKeyProperty la asigne como [paDialog]
En la clase TSubKeyProperty necesito pasar el valor que asigne en la clase TKeyProperty a una forma que abro en el procedimiento Edit del TSubKeyProperty pero no se como realizar esto.

Código Delphi [-]
 
procedure TSubKeyProperty.Edit;
begin
  frmSubKey := TfrmSubKey.Create(Application);
  try
    frmSubKey.Caption := 'Selecciona SubKey';
    frmSubKey.FKey := //?????? <------------ 
    frmSubKey.ShowModal;
    if frmSubKey.ModalResult = mrOK then begin
       SetStrValue(frmSubKey.stSubKey.Caption);
       designer.Modified;
    end;
  finally
    frmSubKey.Free;
  end;
end;

Intenté hacer esto

Código Delphi [-]
    frmSubKey.FKey := TKeyProperty.GetValue;

Pero me da el siguiente error

Cita:

[Pascal Error] RegEditor_D10.pas(73): E2076 This form of method call only allowed for class methods
Espero alguien me pueda ayudar.

Salud OS y gracias.

dec 31-12-2007 20:34:19

Hola,

Me pierdo. Si se mostrase el código completo acaso podría hacerme una idea. Si instancias la clase "TKeyProperty", tal vez podrías acceder a dicha instancia y recuperar el valor que te interese, previamente guardado en la clase "TKeyProperty", es decir, utilizando una variable privada y una propiedad pública para acceder a la misma.

Tal vez no te quede otra que utilizar una variable "global", a la que pudieras acceder tanto desde una clase como desde la otra. Tal vez el asunto pueda reconsiderarse de algún modo para evitar esto. La verdad es que me pierdo, ya lo he dicho. A lo mejor algún otro compañero, antes de tomarse las uvas (y el champán) puede decir algo. ¡O después! ;)

egostar 31-12-2007 20:48:06

Hola Dec

Este es el código

Código Delphi [-]
 
unit RegEditor_D10;
 
interface
 
uses Windows, Forms, Controls, Classes, DesignIntf, DesignEditors, Dialogs,
     RegEditor, USubKey;
 
Type
  TKeyProperty = class (TStringProperty)
    function GetAttributes : TPropertyAttributes; override;
    procedure GetValues(Proc : TGetStrProc); override;
    function GetValue : string; override;
    procedure SetValue(const Value: string); override;
  end;
 
  TSubKeyProperty = class(TPropertyEditor)
    function GetAttributes : TPropertyAttributes; override;
    function GetValue : string; override;
    procedure Edit; override;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterPropertyEditor(TypeInfo(String),TRegEditor,'Key',TKeyProperty);
  RegisterPropertyEditor(TypeInfo(String),TRegEditor,'SubKey',TSubKeyProperty);
  RegisterComponents('Samples', [TRegEditor]);
end;
 
function TKeyProperty.GetAttributes : TPropertyAttributes;
begin
  Result:=[paValueList];
end;
 
Procedure TKeyProperty.GetValues(Proc : TGetStrProc);
begin
  Proc('HKEY_CLASSES_ROOT');
  Proc('HKEY_CURRENT_USER');
  Proc('KEY_LOCAL_MACHINE');
  Proc('HKEY_USERS');
  Proc('HKEY_CURRENT_CONFIG');
end;
 
function TKeyProperty.GetValue : string;
begin
  Result:= GetStrValue;
end;
 
procedure TKeyProperty.SetValue(const Value: string);
begin
  SetStrValue(Value);
  designer.Modified;
end;
 
function TSubKeyProperty.GetAttributes : TPropertyAttributes;
begin
  Result:=[paDialog];
end;
 
function TSubKeyProperty.GetValue : string;
begin
  Result:= GetStrValue;
end;
 
procedure TSubKeyProperty.Edit;
begin
  frmSubKey := TfrmSubKey.Create(Application);
  try
    frmSubKey.Caption := 'Selecciona SubKey';
    // Aqui quiero pasar el valor de la Key a la forma frmSubKey
    frmSubKey.ShowModal;
    if frmSubKey.ModalResult = mrOK then begin
       SetStrValue(frmSubKey.stSubKey.Caption);
       designer.Modified;
    end;
  finally
    frmSubKey.Free;
  end;
end;
 
end.

Gracias

Salud OS

dec 31-12-2007 21:00:01

Hola,

No estoy seguro, pero, parece lógico que pudieras acceder a las propiedades del componente, alguna de cuyas propiedades estés editando. Este artículo de Zarko Gajic tal vez indique que esto es así, puesto que en el mismo puede verse a un editor de propiedades accediendo a un "Component", que, si no me equivoco, debe ser el que se está editando, precisamente. Si es así, podrías acceder desde "TSubKeyProperty" a ese "Component", de modo que pudieras tomar el valor de la propiedad que te interese. Echa un vistazo a ver qué tal. ;)

PD. Mirando en la ayuda se puede ver que los editores de propiedades cuentan con un método "GetComponent", que creo que podría usarse aquí como una posible solución, pero, ignoro si es la más elegante... desde luego sería mejor que utilizar una variable global, o tratar de acceder a la instancia de "TKey..." tal como dije antes:

Cita:

Empezado por Ayuda de Delphi
Call GetComponent to access any of the objects with a property currently being edited by the property editor. The Index parameter is used to specify individual objects in the internal list maintained by the property editor. Index should be a value between 0 and PropCount - 1, inclusive.


egostar 31-12-2007 21:10:59

Muchas gracias Dec, voy a intentar por ambas opciones, ya te dire que pasó, que pases un excelente fin de año.

Salud OS

PD. Nos veremos el año próximo. :)

ariefez 31-12-2007 21:23:16

Ignoro la estructura del componente pero como dice dec se puede acceder con GetComponent

Código Delphi [-]
procedure RegisterPropertyEditor(PropertyType: PTypeInfo; ComponentClass: TClass; const PropertyName: string; EditorClass: TPropertyEditorClass);

basandose en la forma q registras los editores
Código Delphi [-]
procedure Register;
begin
  RegisterPropertyEditor(TypeInfo(String),TRegEditor,'Key',TKeyProperty);
  RegisterPropertyEditor(TypeInfo(String),TRegEditor,'SubKey',TSubKeyProperty);
  RegisterComponents('Samples', [TRegEditor]);
end;
el componente en si seria:
Código Delphi [-]
TRegEditor(GetComponent(0))

si se esta trabajando con colecciones seria bueno q ComponentClass sea tu clase de TCollectionItem para evitar el manejo de indices.

egostar 31-12-2007 21:47:56

Hola ariefez

Muchas gracias, ya hice lo que me comentaste

Código Delphi [-]
 
    frmSubKey.stKey.Caption := TRegEditor(GetComponent(0).Name;

Pero no me coloca nada en el frmSubKey.stKey.Caption.

Seguire intentando, ya les dejaré saber los resultados. Es hora de salir con la familia.

Feliz 2008.

Salud OS

egostar 01-01-2008 02:43:05

Hola amigos

Ya solucione el detalle de pasar el valor de la propiedad de mi componente :).

Código Delphi [-]
procedure TSubKeyProperty.Edit;
begin
  frmSubKey := TfrmSubKey.Create(Application);
  try
    frmSubKey.Caption := 'Selecciona SubKey';
    frmSubKey.stKey.Caption := TRegEditor(GetComponent(0)).Key; 
    frmSubKey.ShowModal;
    if frmSubKey.ModalResult = mrOK then begin
       SetStrValue(frmSubKey.stSubKey.Caption);
       designer.Modified;
    end;
  finally
    frmSubKey.Free;
  end;
end;

Usé lo que me recomendaron GetComponent(0) pero con el nombre de la propiedad del componente (Key).

Salud OS y gracias


La franja horaria es GMT +2. Ahora son las 15:59:54.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi