Delphi estate

Asked

Viewed 333 times

10

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 serial port. In Object Inspector when setting Network Link I would like you to show only the IP and Port properties, if it were changed to Serial show only the Portaserial property. It is possible?

  • Hello Patrick, welcome to [en.so]. Are you willing to change which properties will be visible according to the type of connection? If that is it, you can [Dit] the question by adding this information to make it clear so that we can help you.

  • Oops! Amended question @Pagenotfound

  • 2

    It is not possible. You could disable filling out information through Get and Set. Ex: Getip, Setip and implement control within these methods. Another way is to create 2 components for configuration, and use them by composition within your class. This way, you control the instantiation of one or the other depending on the case

  • Thanks @Caputo That’s right. I’ve changed the question.

  • @Pagenotfount in the case I would define in the Typo Set the destruction and creation of these components?

  • If you choose this way, yes.

  • Dear @Pagenotfound I added the two components as suggested and when setting Tipoconexao set the creation of the associated component and works satisfactorily thanks for the tip. The only thing is that the property is still visible and cannot be changed. You have some other alternative?

  • What’s wrong with being shown?

  • The most correct way would be to create a custom property editor, inheriting from Tpropertyeditor. Read this article. In this custom editor you can do whatever you want, including a Form with a selector with its options and configure panels to be shown at each option. When choosing and setting options, apply them to the component at runtime. Search more using the term Propertyeditor

  • Remember to make sure your settings are being saved when you close Delphi and open it again.

  • Okay, thank you!!!

  • How about doing it this way? This looks like a typical interface situation. http://www.devmedia.com.br/desmistificando-as-interfaces/347

Show 7 more comments

1 answer

7


So!! Following @Pagenotfound’s tips I did it this way:

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 to 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 Settypoconexao I define which component should be active, thus:

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 Stroy. Thank you all!

  • The . dfm is saving your options when closing/opening Delphi again?

  • 1

    @Pagenotfound Yes, changes to properties are being stored in . dfm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.