Delphi property

8

I have a component that has the function of connecting to a specific hardware. It connects through the network or serial port. Something like the code below:

  TConexao = (conRede, conSerial);

  THardware = class(TComponent)
  private
    FAtivo: Boolean;
    FPortaSerial: string;
    FTipoConexao: TConexao;
    FPorta: Integer;
    FIP: string;
    procedure SetAtivo(const Value: Boolean);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure Ativar;
    procedure Desativar;

  published
    property Ativo: Boolean read FAtivo write SetAtivo;
    property TipoConexao: TConexao read FTipoConexao write FTipoConexao;
    property IP: string read FIP write FIP;
    property Porta: Integer read FPorta write FPorta;
    property PortaSerial: string read FPortaSerial write FPortaSerial;
  end;

To connect through the network I use the IP and Port properties and to connect through the serial I use only the serial port. In the Object Inspector when setting ConnectionType to Network you would like to display only the IP and Port properties, if it were changed to Serial only show the PortaSerial property. It's possible?

    
asked by anonymous 21.10.2014 / 12:44

1 answer

6

So !! Following the tips from @PageNotFound I did the following:

First I created a component for each connection:

  TRede = class(TComponent)
  private
    FPorta: Integer;
    FIP: string;
  published
    property IP: string read FIP write FIP;
    property Porta: Integer read FPorta write FPorta;
  end;

  TSerial = class(TComponent)
  private
    FPorta: string;
  published
    property Porta: string read FPorta write FPorta;
  end;

Then I added the properties in the main component:

  THardware = class(TComponent)
  private
    FAtivo: Boolean;
    FTipoConexao: TTipoConexao;
    FRede: TRede;
    FSerial: TSerial;

    procedure SetAtivo(const Value: Boolean);
    procedure SetTipoConexao(const Value: TTipoConexao);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;

    procedure Ativar;
    procedure Desativar;

  published
    property Ativo: Boolean read FAtivo write SetAtivo;
    property TipoConexao: TTipoConexao read FTipoConexao write SetTipoConexao default conRede;
    property Rede: TRede read FRede write FRede;
    property Serial: TSerial read FSerial write FSerial;
  end;

And in the SetTipoConnection it defines which component should be active, like this:

procedure THardware.SetTipoConexao(const Value: TTipoConexao);
begin
  if FTipoConexao <> Value then
  begin
    case Value of
      conRede:
      begin
        FreeAndNil(FSerial);
        FConexao := TRede.Create(self);
        FConexao.Name := 'Rede';
      end;
      conSerial:
      begin
        FreeAndNil(FRede);
        FConexao := TSerial.Create(self);
        FConexao.Name := 'Serial';
      end;
    end;

    FTipoConexao := Value;
  end;
end;

Remember to create the components in create and destroy them in destroy. Thank you all!

    
21.10.2014 / 19:02